Nearby lessons

10 of 19

Hibernate - Creating SessionFactory (Hibernate 4.x)

📌 What You Will Learn
  • Understand Hibernate - Creating SessionFactory (Hibernate 4.x)
  • See working code examples
  • Learn from common mistakes and Q&A

Learn Hibernate - Creating SessionFactory (Hibernate 4.x) step by step — simple explanations, complete programs with their output, common beginner mistakes, and exam-style MCQs.

Creating SessionFactory (Hibernate 4.x)

Hibernate Schema Generation Tools:

In Hibernate applications, to perform DML operations like insert, update, delete ,... we will use the methods lile save(), update(), delete(),... from org.hibernate.Session interface provided by Hibernate Software.

In Hibernate applications, to perform DDL operations like create, alter, drop,... Hibernate has provided the following implicit tools.

  • SchemaExport
  • SchemaUpdate
  • CodeGeneration

Note: The above Shema generation tools are usefull to create tables when we want to work with unknown databases.

SchemaExport — Customer.java

This tool is provided by Hibernate software in the form of org.hibernate.tool.hbm2ddl.SchemaExport predefined class in hibernate3.jar file.

If we activate this tool then it will perform the following actions.

  • First SchemaExport tool will take name and location of hibernate configuration file which we have provided as input parameter.
  • SchemaExport tool will load and parse hibernate configuration file.
  • SchemaExport tool will recognize mapping file configuration in hibernate configuration file

then it will load and parse mapping file.

  • As per mapping file details, SchemaExport tool will check whether any table is existed or

not in Database.

  • If no table is existed then SchemaExport tool will create new table as per mapping file

details.

  • If any table is existed with the name then SChemaExport tool will drop that existed table

and creates new table as per mapping file configuration details.

To activate SchemaExport tool manually through command prompt then we have to use the following command on command prompt.

D:\apps>java org.hibernate.tool.hbm2ddl.SchemaExport --config =hibernate.cfg.xml Note: To run the above command we must copy all hibernate jars to "C:\Java\jdk1.7.0\jre\lib\ext" folder and we must provide POJO class, mapping file and configuration file at the same location from where we are activating SchemaExport tool.

Example:

Example02
JCode Cell
1 
2public class Customer
3{
4private String cid;
5private String cname;
6private String caddr;
7private String cemail;
8 
9public void setCid(String cid)
10{
11this.cid = cid;
12}
13public void setCname(String cname)
14{
15this.cname = cname;
16}
17public void setCaddr(String caddr)
18{
19this.caddr = caddr;
20}
21public void setCemail(String cemail)
22{
23this.cemail = cemail;
24}
25 
26public String getCid()
27{
28return cid;
29}
30public String getCname()
31{
32return cname;
33}
34public String getCaddr()
35{
36return caddr;
37}
38public String getCemail()
39{
40return cemail;
41}
42}
43

SchemaExport — Customer.hbm.xml

Example03
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="Customer" table="customer">
8<id name="cid" column="CID" length="5" type="string"/>
9<property name="cname" column="CNAME" length="10" type="string"/>
10<property name="caddr" column="CADDR" length="10" type="string"/>
11<property name="cemail" column="CEMAIL" length="20" type="string"/>
12</class>
13</hibernate-mapping>
14

SchemaExport — hibernate.cfg.xml

Example04
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="Customer.hbm.xml"/>
15</session-factory>
16</hibernate-configuration>
17

Command on Command prompt

D:\apps>javac Customer.java D:\apps>java org.hibernate.tool.hbm2ddl.SchemaExport --config =hibernate.cfg.xml

SchemaUpdate — Customer.java

This tool is provided by Hibernate software in the form of org.hibernate.tool.hbm2ddl.SchemaUpdate predefined class in hibernate3.jar file.

If we activate this tool then it will perform the following actions.

a) First SchemaUpdate tool will take name and location of hibernate configuration file which we have provided as input parameter.

b) SchemaUpdate tool will load and parse hibernate configuration file. c) SchemaUpdate tool will recognize mapping file configuration in hibernate configuration file

then it will load and parse mapping file. d) As per mapping file details, SchemaUpdate tool will check whether any table is existed or

not in Database with the same name. e) If no table is existed then SchemaUpdate tool will create new table as per mapping file

details. f) If any table is existed with the name then SChemaUpdate tool will alter that existed table as

per mapping file configuration details.

Note: When mapping contains new colums details when compared with database table then only SchemaUpdate tool will perform alter operation. If Database table contains more columns when compared with mapping file configuration details then SchemaUpdate tool will not perform alter operation.

To activate SchemaUpdate tool manually through command prompt then we have to use the following command on command prompt.

D:\apps>java org.hibernate.tool.hbm2ddl.SchemaUpdate --config =hibernate.cfg.xml

Note: To run the above command we must copy all hibernate jars to "C:\Java\jdk1.7.0\jre\lib\ext" folder and we must provide POJO class, mapping file and configuration file at the same location from where we are activating SchemaUpdate tool.

Example06
JCode Cell
1 
2public class Customer
3{
4private String cid;
5private String cname;
6private String caddr;
7private String cemail;
8 
9public void setCid(String cid)
10{
11this.cid = cid;
12}
13public void setCname(String cname)
14{
15this.cname = cname;
16}
17public void setCaddr(String caddr)
18{
19this.caddr = caddr;
20}
21public void setCemail(String cemail)
22{
23this.cemail = cemail;
24}
25 
26public String getCid()
27{
28return cid;
29}
30public String getCname()
31{
32return cname;
33}
34public String getCaddr()
35{
36return caddr;
37}
38public String getCemail()
39{
40return cemail;
41}
42}
43

SchemaUpdate — Customer.hbm.xml

Example07
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="Customer" table="customer">
8<id name="cid" column="CID" length="5" type="string"/>
9<property name="cname" column="CNAME" length="10" type="string"/>
10<property name="caddr" column="CADDR" length="10" type="string"/>
11<property name="cemail" column="CEMAIL" length="20" type="string"/>
12</class>
13</hibernate-mapping>
14

SchemaUpdate — hibernate.cfg.xml

Example08
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="Customer.hbm.xml"/>
15</session-factory>
16</hibernate-configuration>
17

CodeGeneration

The mian intention of CodeGeneration tool is to create POJO class as per database table configurations which we provided in mapping file.

Note: This is available upto Hibernate2.x version, but, it is deprecated in Hibernate3.x version.

In Hibernate applications, we are able to activate SchemaExport and SchemaUpdate tools in declarative manner also, for this, we have to use the following property in hibernate configuration file.

hibernate.hbm2ddl.auto

The above property will take the following three values.

  • create
  • update
  • create-drop
  • Create: This option for "hibernate.hbm2ddl.auto" property will activate SchemaExport tool, it will create a table irrespective of the table is existed or not.
Example10
JCode Cell
1 
2<hibernate-configuration>
3----
4<property name="hibernate.hbm2ddl.auto">create</property>
5----
6</hibernate-configuration>
7

hibernate.hbm2ddl.auto

  • Update: This option for "hibernate.hbm2ddl.auto" property will activate SchemaUpdate tool, it will create table if table is not existed, it will alter the table if table is existed as per the mapping file.
Example11
JCode Cell
1 
2<hibernate-configuration>
3----
4<property name="hibernate.hbm2ddl.auto">update</property>
5----
6</hibernate-configuration>
7

hibernate.hbm2ddl.auto

  • Create-Drop: This option for "hibernate.hbm2ddl.auto" will create table when SessionFactory object is created and it will drop table when SessionFactory object is closed.
Example12
JCode Cell
1 
2<hibernate-configuration>
3----
4<property name="hibernate.hbm2ddl.auto>create-drop</property>
5----
6</hibernate-configuration>
7

hibernate.hbm2ddl.auto

Creating SessionFactory object in Hibernate4.x version:

In Hibernate4.x version, cfg.buildSessionFactory() method was deprecated, because, to load hibernate software and to setup Hibernate environment it may take lot of time. To simplify Hibernate Software loading and to set up Hibernate Environment in simplified manner, Hibernate4.x version has included some booting features .

In Hibernate4.x version, to create SessionFactory object we have to use the following steps.

a) Create Configuration Object with all configuration details

Configuration cfg = new Configuration(); cfg.configure();

b) Get all configuration details into Properties object

public Properties getProperties() EX: Properties p = cfg.getProperties();

c) Creating StandardServiceRegistryBuilder

StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder();

d) Get All configuration details from Properties object into

StandardServiceRegistryBuilder object

public StandardServiceRegistryBuilder applySettings(Map m) EX: builder = builder.applySettings(p);

e) Create StandardServiceRegistry object

public StandardServiceRegistry build() EX: StandardServiceRegistry registry = builder.build();

f) Create SessionFactory object

public SessionFactory buildSessionFactory(StandardServiceRegi stry registry)

EX: Sessionfactory sf = cfg.buildSessionfactory(registry);

📝 Key Takeaways
  • Key ideas of Hibernate - Creating SessionFactory (Hibernate 4.x) explained simply
  • Ready-to-use code examples
  • Exam-style questions at the end