Hibernate SessionFactory

Facebooktwitterredditpinterestlinkedinmailby feather

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:

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