Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Hibernate hello world

In this section, you will learn about basic tutorial of inserting value in the database table and printing "hello world" on the console after successful insertion.

Ads

Tutorials   
How to write hello world example program in Hibernate? How to create one to one relationship in Hibernate 4 with Annotation? How to calculate minimum in Hibernate using the min() Function Hibernate 4.3.4 Released: Hibernate 4.3.4.Final Maven dependency Hibernate 4.3 Hello World: Example How to create SessionFactory in Hibernate 4.3.1? Hibernate 4 Hello World: Example of Hello World program Hibernate 4 create Session Factory: Example of creating Session Factory in Hibernate 4 Hibernate 4.3 and JPA 2.1 Example What is Hibernate in Java? Hibernate Overview and Architecture What is JPA 2.1? Hibernate 4.3.0.Final Maven dependency Hibernate 4.3 ORM Features Hibernate 4.3 Tutorial Hibernate Criteria average example Hibernate Criteria load all objects from table Hibernate Criteria Hibernate beginner tutorial Hibernate Session.delete() Example Hibernate save or update method example Hibernate Load Example Hibernate save Example - Rose India Hibernate 4 tutorials Hibernate Example Step by Step in Eclipse Hibernate Configuration files Setup Hibernate Environment Hibernate Architecture Hibernate Overview Hibernate 4.2 Tutorial Hibernate 4 Annotations Tutorial Hibernate 4 AnnotationSessionFactoryBean hibernate versions hibernate jndi hibernate tomcat hibernate metamodel generator hibernate entity Seam Hibernate C3P0 Hibernate hibernate shard hibernate means temporal hibernate jsf hibernate spring j2ee hibernate spring hibernate encrypted password alias_to_entity_map hibernate temporal jsf spring hibernate hibernate by example hibernate repository hibernate sample code
Ads

Hibernate hello world

In this section, you will learn about basic tutorial of inserting value in the database table and printing "hello world" on the  console after successful insertion.

EXAMPLE

In the below example, we are inserting a record and after successful insertion a Hibernate Hello World Example message will be displayed.

The project structure and jar file used is given below :

The query used to create employee table is given below :

CREATE TABLE `employee` ( 
`Emp_code` int(11) NOT NULL auto_increment, 
`Emp_name` varchar(30) default NULL, 
PRIMARY KEY (`Emp_code`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 

CODE

The annotated Employee entity class is given below :

package net.roseindia;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "employee")
public class Employee {
@Id
@GeneratedValue
@Column(name = "Emp_code")
private int EmpCode;

@Column(name = "Emp_name")
private String EmpName;

public int getEmpCode() {
return EmpCode;
}

public void setEmpCode(int empCode) {
EmpCode = empCode;
}

public String getEmpName() {
return EmpName;
}

public void setEmpName(String empName) {
EmpName = empName;
}
}

hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://192.168.10.13:3306/ankdb</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.current_session_context_class">thread</property>

<mapping class="net.roseindia.Employee" />

</session-factory>
</hibernate-configuration>

HibernateUtil.java

Using this class, we have created SessionFactory  instance :

package net.roseindia;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateUtil {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
Configuration configuration = new Configuration();
configuration.configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); 
return configuration.buildSessionFactory(serviceRegistry);

}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}

public static SessionFactory getSessionFactory() {
sessionFactory=buildSessionFactory();
return sessionFactory;
}

public static void shutdown() {
// Close caches and connection pools
getSessionFactory().close();
}
}

ManageEmployee.java

package net.roseindia;


import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

public class ManageEmployee {
public static void main(String args[]){
Session session=HibernateUtil.getSessionFactory().openSession();
try{
session.beginTransaction();
Employee emp=new Employee();
emp.setEmpName("Rajesh Koothrapalli");

session.save(emp);
session.getTransaction().commit();

//Displaying inserted value
Query elist = session.createQuery("from Employee as emp where emp.EmpName = 'Rajesh Koothrapalli'");
List<Employee> empList = elist.list();
for (Employee e : empList) {
System.out.println("Employee ID : " + e.getEmpCode());
System.out.println("Employee Name : " + e.getEmpName());
}
}catch (Exception e) {
e.printStackTrace();
}finally{
session.close();
}
System.out.println("Hibernate Hello World Example");
}
}

OUTPUT

After executing ManageEmployee.java code, you will get the following output in console :

Hibernate: insert into employee (Emp_name) values (?)
Hibernate: select employee0_.Emp_code as Emp1_0_, employee0_.Emp_name as Emp2_0_ from employee employee0_ where employee0_.Emp_name='Rajesh Koothrapalli'
Employee ID : 1
Employee Name : Rajesh Koothrapalli
Hibernate Hello World Example

In database table Employee , you will find the inserted record :

Download Source Code

Advertisements

Ads
Share on Google+Share on Google+

Hibernate hello world

Posted on: April 27, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Hibernate hello world  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads