Nearby lessons
10 of 19Hibernate - Creating SessionFactory (Hibernate 4.x)
- 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:
SchemaExport — Customer.hbm.xml
SchemaExport — hibernate.cfg.xml
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.
SchemaUpdate — Customer.hbm.xml
SchemaUpdate — hibernate.cfg.xml
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.
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.
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.
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 ideas of Hibernate - Creating SessionFactory (Hibernate 4.x) explained simply
- Ready-to-use code examples
- Exam-style questions at the end