Hibernate SessionFactory

Hibernate SessionFactory

A SessionFactory is an interface in Hibernate that create the instances of Session interface. It is a heavy weight object, and it is usually created during the application startup and kept for later use. It is a threadsafe object and used by all the threads in the application. The SessionFactory interface is available in the org.hibernate.sessionFactory and can be obtained by the Configuration object.

There is only one SessionFactory object for an application. If our application is using multiple databases, there will be one SessionFactory object per database. It also holds the second-level cache data in Hibernate.

We can create the SessionFactory object by the following code:

Configuration configuration = new Configuration().configure();
SessionFactory factory = configuration.buildSessionFactory(); 

Methods of SessionFactory interface

The SessionFactory interface provides a variety of methods. Some commonly used methods are listed below:           

Methods Description
public void close()   This method is used to destroy the current SessionFactory and releases all the resources (cache, connection pools). If the SessionFactory is already closed, no-operation will be performed.  
public Map getAllClassMetadata()   This method is used to retrieve the ClassMetadata for all mapped entities. It will return a Map containing all ClassMetadata mapped by the corresponding String entityname.  
public Map getAllCollectionMetadata()   This method is used to retrieve the CollectionMetadata for all mapped collections. It returns a Map from String to CollectionMetadata.  
public Cache getCache()   This method can directly access the underlying cache regions.
public ClassMetadata getClassMetadata(Class entityClass)   This method is used to retrieve the ClassMetadata associated with the given entity class. The Metadata will become null if there is no entity mapped with it.  
public CollectionMetadata getCollectionMetadata(String roleName) This method is used to retrieve the CollectionMetadata associated with the named collection role. If no such Collection is mapped with the Metadata, then it will become null.  
public Session getCurrentSession()   This method is used to obtain the current Hibernate Session and associates the Session with the current Session.  
public Statistics getStatistics()   This method is used to retrieve the statistics for this SessionFactory.  
public boolean isClosed()   This method is used to check whether the SessionFactory is open or closed. It will return true if the factory is already closed else false.  
public Session openSession()   This method is used to open a Session. The configured connection provider will provide the database connections when needed.  
public Session openSession(Connection connection)   This method is used to open a Session using the specified database connection. When we supply a JDBC connection, then the second-level cache will be automatically disabled.  
public Session openSession(Interceptor interceptor)   This method is used to open a Session, using the specified interceptor. An interceptor allows user code to intercept or change the property values. The configured connection provider will provide the database connections when needed.          
public StatelessSession openStatelessSession() This method is used to open a new stateless session using the specified database connection. A stateless Session has no persistent object associated with it. It does not implement a first-level cache and a second-level cache.

Related Topics

Hibernate Inheritance Joined Table

In this inheritance strategy, mapping of the child class fields is done with the common fields of the parent class in a separate table. In other words, the common entities between the child...

3 minutes read.

Hibernate Composite Primary Key

Hibernate Composite Primary Key A Composite Primary Key is a combination of one or more columns that forms a primary key. When a database table contains more than one primary key column, it is...

8 minutes read.

Hibernate Session Interface

Hibernate Session Interface A Session is an interface between the Java application and the Hibernate. It is available in the org.hibernate.session package. It should not be kept open for a long time, as it...

4 minutes read.

Hibernate Named Query using Annotation

In annotation-based named query, we use @NamedQuery annotation inside the POJO class. @NamedQuery- It is used to describe a single named query. It consists of four attributes- two of which are compulsory, and...

2 minutes read.

Hibernate Inheritance - Single Table

Hibernate Inheritance In this inheritance strategy, only one table is created for all the classes involved in the hierarchy with an additional column known as a discriminator column. The discriminator column helps indifferentiating between...

3 minutes read.

Hibernate Many-to-One Mapping

Many-to-One Hibernate Mapping with Example A Many-to-One association mapping is the reverse of One-to-Many association mapping. For example, many (customers) are associated with one (vendor). In Hibernate, Many-to-One association mapping is applied from child class...

3 minutes read.

Hibernate Features

1. Lightweight Hibernate is a lightweight framework as it does not contains additional functionalities; it uses only those functionalities required for object-relational mapping. It is a lightweight framework because it uses persistent...

2 minutes read.

Hibernate Caching

What is Caching? Caching is a process of saving data into the cache memory. A cache is a temporary storage layer which is used to increase the speed of data access. It allows...

2 minutes read.

Hibernate save() vs persist()

Session.save() The session.save() method is used to save the transient object by assigning a generated value or using a current value of the identifier property. In other words, the save() method is used...

3 minutes read.

Hibernate One-to-Many Mapping

One-to-Many Hibernate Mapping Example In One-to-many association mapping, only one object of a persistent class is related to many objects of another persistent class. It is a relationship in which one (parent) is related...

3 minutes read.

Hibernate Inverse

Hibernate Inverse An Inverse attribute is used to maintain the relationship between the parent and child class object. The inverse attribute is used only with bi-directional mappings such as one-to-many and many-to-many Hibernate mapping. In Hibernate, the...

2 minutes read.

Hibernate Inheritance Table Per Class

In this inheritance strategy, table per class is generated. It means a separate table is generated for each POJO class involved in the hierarchy. Unlike the SINGLE_TABLE strategy, there are no nullable values...

3 minutes read.

Hibernate Tutorial

What is Hibernate? Hibernate is an Object-Relational Mapping (ORM) tool which reduces the difficulty in the application development. Hibernate provides a framework that interacts with the data stored in the databases. It also uses...

3 minutes read.

Hibernate Named Query Using XML

A named query is a technique used to assemble all the queries (Native SQL and HQL) in a specific location and refer them by some name. It helps in reducing the mess...

3 minutes read.

Hibernate get() vs. load()

Hibernate methods, get() and load() both are provided by the Session interface and used to retrieve the objects from the database. Session.get() The session.get() method is used to fetch a persistent object of the...

2 minutes read.

Hibernate Merge

Hibernate Merge Merge is a method available in Hibernate which is used to update the existing records. However, it creates a copy from the passed entity objects and returns it. In other words, the merge()...

2 minutes read.

Hibernate Dialects

Hibernate Dialects Dialect is a Java class available in org.hibernate.dialect package, which helps to map Java Application with the database. To interact with the database, we need to define the required database dialect in...

2 minutes read.

Hibernate Annotation with Example

Hibernate is an ORM (Object-Relational Mapping) tool which simplifies the application development. Hibernate provides a framework which interacts with the data stored in the databases and it also uses the specifications of the...

2 minutes read.

Hibernate Web Application Example

Web Application Example with Hibernate In this section, we create a hibernate web Application. Here we are using a JSP file for presentation. Example of creating a web application using hibernate index.jsp This page will show a...

2 minutes read.

Hibernate first Example

Now we are going to create the first example in Hibernate. We will proceed step by step to develop the first Java application in Hibernate. The steps are listed below:- Create the POJO...

2 minutes read.