Nearby lessons

9 of 19

Hibernate - Schema Generation Tools

📌 What You Will Learn
  • Understand Hibernate - Schema Generation Tools
  • See working code examples
  • Learn from common mistakes and Q&A

Learn Hibernate - Schema Generation Tools step by step — simple explanations, complete programs with their output, common beginner mistakes, and exam-style MCQs.

Schema Generation Tools — Employee.java

 Where "name" attribute will take id property name and column attribute will take "database table column name".

EX:Employee.hbm.xml

  • <hibernate-mapping>
  • <class name="com.durgasoft.hbn.pojo.Employee" table="emp1">
  • <composite-id>
  • <key-property name="eno" column="ENO"/>
  • <key-property name="ename" column="ENAME"/>
  • </composite-id>
  • <property name="esal" column="ESAL"/>
  • <property name="eaddr" column="EADDR"/>
  • </class>
  • </hibernate-mapping>
  • Note: To declare compisite keys in annotated classes , no need to use any seperate Annot

ation, simply we have to declare @Id annotation at both the columns.

  • EX: Employee.java
  • @Entity
  • @Table(name="emp1")
  • public class Employee implements Serializable {
  • @Id
  • @Column(name="ENO")
  • private int eno;
  • @Id
  • @Column(name="ENAME")
  • private String ename;
  • @Column(name="ESAL")
  • private float esal;
  • @Column(name="EADDR")
  • private String eaddr;
  • setXXX() and getXXX()

Note: To use Composite keys feature in Hibernate applications then it is mandatory to implement java.io.Serializable marker interface to POJO class.

Example:

Example01
JCode Cell
1 
2package com.durgasoft.hbn.pojo;
3 
4import java.io.Serializable;
5 
6import javax.persistence.Column;
7import javax.persistence.Entity;
8import javax.persistence.Id;
9import javax.persistence.Table;
10@Entity
11@Table(name="emp1")
12public class Employee implements Serializable {
13@Id
14@Column(name="ENO")
15private int eno;
16@Id
17@Column(name="ENAME")
18private String ename;
19@Column(name="ESAL")
20private float esal;
21@Column(name="EADDR")
22private String eaddr;
23 
24public int getEno() {
25return eno;
26}
27public void setEno(int eno) {
28this.eno = eno;
29}
30public String getEname() {
31return ename;
32}
33public void setEname(String ename) {
34this.ename = ename;
35}
36public float getEsal() {
37return esal;
38}
39public void setEsal(float esal) {
40this.esal = esal;
41}
42public String getEaddr() {
43return eaddr;
44}
45public void setEaddr(String eaddr) {
46this.eaddr = eaddr;
47}
48 
49 
50}
51

Schema Generation Tools — Employee.hbm.xml

Example02
JCode Cell
1 
2<?xml version="1.0" encoding="UTF-8"?>
3<!DOCTYPE hibernate-mapping PUBLIC
4"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
5"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
6<hibernate-mapping>
7<class name="com.durgasoft.hbn.pojo.Employee" table="emp1">
8 <composite-id>
9 <key-property name="eno" column="ENO"/>
10 <key-property name="ename" column="ENAME"/>
11</composite-id>
12<property name="esal" column="ESAL"/>
13<property name="eaddr" column="EADDR"/>
14</class>
15</hibernate-mapping>
16

Schema Generation Tools — hibernate.cfg.xml

Example03
JCode Cell
1 
2<?xml version="1.0" encoding="UTF-8"?>
3<!DOCTYPE hibernate-configuration PUBLIC
4"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
5"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
6<hibernate-configuration>
7<session-factory>
8<property name="connection.driver_Class">oracle.jdbc.OracleDriver</property>
9<property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
10<property name="connection.user">system</property>
11<property name="connection.password">durga</property>
12<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
13<property name="show_sql">true</property>
14<!-- <mapping resource="Employee.hbm.xml"/> -->
15<mapping class="com.durgasoft.hbn.pojo.Employee"/>
16</session-factory>
17</hibernate-configuration>
18

Schema Generation Tools — ClientApp.java

Example04
JCode Cell
1 
2package com.durgasoft.hbn.test;
3 
4 
5import org.hibernate.Session;
6import org.hibernate.SessionFactory;
7import org.hibernate.Transaction;
8import org.hibernate.cfg.Configuration;
9 
10import com.durgasoft.hbn.pojo.Employee;
11 
12public class ClientApp {
13 
14public static void main(String[] args) {
15SessionFactory sessionFactory = null;
16Session session = null;
17try {
18 
19 Configuration cfg = new Configuration();
20 cfg.configure();
21 sessionFactory = cfg.buildSessionFactory();
22 session = sessionFactory.openSession();
23 
24 Employee emp1 = new Employee();
25 emp1.setEno(222);
26 emp1.setEname("BBB");
27 emp1.setEsal(8000);
28 emp1.setEaddr("Hyd");
29 Transaction tx = session.beginTransaction();
30 session.save(emp1);
31 tx.commit();
32 System.out.println("Employee Inserted Successfully");
33 
34 /*
35 Employee emp_Key = new Employee();
36 emp_Key.setEno(222);
37 emp_Key.setEname("XXX");
38 Employee emp = (Employee) session.get(Employee.class, emp_Key);
39 if (emp == null) {
40 System.out.println("Employee Not Existed");
41 } else {
42 System.out.println("Employee Details");
43 System.out.println("====================");
44 System.out.println("Employee Number :" + emp.getEno());
45 System.out.println("Employee Name :" + emp.getEname());
46 System.out.println("Employee Salary :" + emp.getEsal());
47 System.out.println("Employee Address :" + emp.getEaddr());
48 }
49 */
50 
51} catch (Exception e) {
52 e.printStackTrace();
53} finally {
54 session.close();
55 sessionFactory.close();
56 
57}
58}
59}
60

Hibernate Persistence Object Lifecycle

The collective information of any object right from starting point to end point is called as Object Lifecycle.

In Hibernate Applications, Persistence object is having the following lifecycle states.

  • Transient State
  • Persistence State
  • Ditached State
  • Removed State

Transient State

 In Hibernate Applications, when we create POJO object then automatically POJO object will come to Transient State.

 If POJO object is available in Transient State then Hibernate Software is not aware about POJO object.  If POJO object is available in Transient State then POJO object is representing any record in Database table.

 In this state, Hibernate Software is unable to provide synchronization between POJO object and record in Database.

 In this state, if we perform modifications in POJO object then that modifications are not reflected to any record in Database.

 If we keep POJO object in Transient State for long time then POJO object is eligible for Garbage Collection then Garbage Collector will destroy POJO object.

Persistence State

 If we perform the Operations like save() , update(), saveOrUpdate(),.... over the POJO object which is available in Transient State then POJO object will come to Persistence State.

 In Hibernate Applications, if we perform the objects like get() , load(), find(),... automatically Hibernate Software will create POJO object in Persistence State directly without Transient State.

 In Persistence State, Hibernate Software is aware about POJO Object and database table record and its synchronization.

 In Persistence State, if we perform modifications on POJO object then that modifications are reflected to Database table record.

Ditached State

 If we perform the operations like close(), clear(), evict(),.... over the Session object then POJO object will come from Persistence State to Ditached State.

 In Ditached State, Hibernate Software is not aware about POJO object and it will not provide synchroinization between POJO object and database table record.

 In Ditached State, POJO object is representing valid record in database table,but, modifications on POJO object are not reflected to that record.  In Ditached State, if we perform operations like save() , update(), saveOrUpdate(),... over

the persistence object after getting Session back then POJO object will transfer from Ditached State to Persistence State again.

 If we keep POJO object in Ditached State for long time then POJO object is eligible for Garbage Collection, where Garbage Collector will destroy that object.

Removed State

 If we perform the operations like remove() or delete() over the POJO object in Persistence state then POJO object will come from Persistence State to Removed State.

 In Removed State, Hibernate Software is not aware about POJO object.

 In Removed State, POJO object is not representing any record in Database table.

 In Hibernate Applications, Once if POJO object is in Removed State then it is not possible to get back into Persistence State, directly, it must go for Garbage Collection only.

Example:

Employee emp = new Employee();// Transient State

---- session.save(emp);// Persistence State ---- ---- session.delete(emp); Removed State

Example:

Account acc = new Account(); // Transient State --- --- session.save(acc); // Persistence State --- ---- session.close(); // Ditached State ---- ---- sessionFactory.getCurrentSession(); or sf.openSession(); ---- session.update(acc); // Persistence

📝 Key Takeaways
  • Key ideas of Hibernate - Schema Generation Tools explained simply
  • Ready-to-use code examples
  • Exam-style questions at the end