org.hibernate.SessionFactory)org.hibernate.Session)java.sql.Connection. Factory for org.hibernate.Transaction. Maintains a first level cache of persistent the application’s persistent objects and collections; this cache is used when navigating the object graph or looking up objects by identifier. org.hibernate.Session. Once the org.hibernate.Session is closed, they will be detached and free to use in any application layer (for example, directly as data transfer objects to and from presentation). org.hibernate.Transaction)org.hibernate.connection.ConnectionProvider)org.hibernate.TransactionFactory)org.hibernate.cfg.Configuration. An instance of org.hibernate.cfg.Configuration represents an entire set of mappings of an application’s Java types to an SQL database. The org.hibernate.cfg.Configuration is used to build an immutable org.hibernate.SessionFactory. The mappings are compiled from various XML mapping files or from Java 5 Annotations. Hibernate provides following types of configurations <?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <!-- a SessionFactory instance listed as /jndi/name -->
    <session-factory
        name="java:hibernate/SessionFactory">
        <!-- properties -->
        <property name="connection.datasource">java:/comp/env/jdbc/MyEmployeeDB</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">false</property>
        <property name="transaction.factory_class">
            org.hibernate.transaction.JTATransactionFactory
        </property>
        <property name="jta.UserTransaction">java:comp/UserTransaction</property>
        <!-- mapping files -->
        <mapping resource="net/viralpatel/hibernate/Employee.hbm.xml"/>
        <mapping resource="net/viralpatel/hibernate/Department.hbm.xml"/>
        <!-- cache settings -->
        <class-cache class="net.viralpatel.hibernate.Employee" usage="read-write"/>
        <class-cache class="net.viralpatel.hibernate.Department" usage="read-only"/>
        <collection-cache collection="net.viralpatel.hibernate.Department.employees" usage="read-write"/>
    </session-factory>
</hibernate-configuration>
Code language: HTML, XML (xml)SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Code language: Java (java)SessionFactory sf = new Configuration()
    .configure("employeedb.cfg.xml")
    .buildSessionFactory();
Code language: Java (java)Note: Both hibernate.cfg.xml and hibernate.properties files can be provided simultaneously in an application. In this case hibernate.cfg.xml gets precedence over hibernate.properties.
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url= jdbc:mysql://localhost:3306/employee
hibernate.connection.username=root
hibernate.connection.password=swordfish
hibernate.connection.pool_size=1
hibernate.transaction.factory_class = \
    org.hibernate.transaction.JTATransactionFactory
hibernate.transaction.manager_lookup_class = \
    org.hibernate.transaction.JBossTransactionManagerLookup
hibernate.dialect = org.hibernate.dialect.MySQLDialect
Code language: Java (java)Configuration cfg = new Configuration()
    .addResource("Employee.hbm.xml")
    .addResource("Department.hbm.xml");
Code language: Java (java)Configuration cfg = new Configuration()
    .addClass(net.viralpatel.hibernate.Employee.class)
    .addClass(net.viralpatel.hibernate.Department.class);
Code language: Java (java)Configuration cfg = new Configuration()
    .addClass(net.viralpatel.hibernate.Employee.class)
    .addClass(net.viralpatel.hibernate.Department.class)
    .setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLInnoDBDialect")
    .setProperty("hibernate.connection.datasource", "java:comp/env/jdbc/test")
    .setProperty("hibernate.order_updates", "true");
Code language: Java (java)Hibernate does allow your application to instantiate more than one org.hibernate.SessionFactory. This is useful if you are using more than one database.Code language: Java (java)SessionFactory sessions = cfg.buildSessionFactory();
Session session = sessions.openSession(); // get a new Session
Code language: Java (java)Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
very good materials for read for newbie using simple word to explain ... keep on the good work... thanks alot!
Great article! Very helpful! Thanks!
Very good article, and very helpfull.. Thanks a lot
Must say , hibernate architecture in a frame ... Thanks a lot!!
very well articulated, must say a nice piece of work!! Truly helpful for me!!
Thanks a lot dude!! keep up good works!!
Excellent tutorials but i have one request please organise tutorials in step by step by manner sometime it is difficult to find out tutorials
very good article...its very useful for me.....thank u so much
Can you show me the package structure of hibernate ... I am beginner in hibernate and i want to know where to place which file in package ...
thanx hactic
Great Thanks a lot. please do provide sample code for hibernate using SQL Joins or an example using Hibernate query language.