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

Annotation Example in Hibernate

Hibernate Annotation 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...

3 minutes read.

Lazy Loading in Hibernate

Lazy loading is a fetching technique used for all the entities in Hibernate. It decides whether to load a child class object while loading the parent class object. When we use association mapping...

3 minutes read.

Hibernate Configuration Properties

Hibernate Configuration Hibernate Configuration is a Java class, which allows a Java application to specify configuration parameters used in the application. As Hibernate is designed to serve in different environments, it needs a broad...

6 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 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 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 History and Versions

Next → History of Hibernate Hibernate was developed in 2001 by Gavin king with his colleagues from Circus Technologies. The main aim was to provide better persistence capabilities than those of EJB2, by reducing the...

2 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 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 N+1 Select Problem

Hibernate N + 1 Select Problem The N + 1 Select problem is a performance issue in Hibernate. In this problem, a Java application makes N + 1 database calls (N = number...

4 minutes read.

Hibernate Many-to-Many Mapping

Many-to-Many Hibernate Mapping with Example In Many-to-Many association mapping, more than one objects of a persistent class are associated with more than one objects of another persistent class. For example, many (categories) are related to...

3 minutes read.

Dirty Checking in Hibernate

Dirty checking is an essential concept of Hibernate. The Dirty checking concept is used to keep track of the objects. It automatically detects whether an object is modified (or not) or wants to...

6 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.

Hibernate Web Application with Example

Let us create a hibernate web Application. Here we need JSP file for presentation. There are steps given below for the web app creation. index.jsp This page will show a registration form. It takes the...

2 minutes read.

Hibernate Second-Level Cache

The Second-level cache is related to the SessionFactory object. Once the SessionFactory is closed, all the second-level cache data associated with it will be lost. The cache manager will also get closed. Cache provider The...

5 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 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 vs. JDBC

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...

2 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 Mapping

Hibernate Mapping Association- It means that two or more things are related to each other by some specific relation. In other words, it specifies how the objects are associated with each other. Hibernate mapping is one...

2 minutes read.