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 :