Example & Tutorial understanding programming in easy ways.

What do you create a SessionFactory?

Hibernate is a ORM tool. With the help of this we can insert, update and delete records from data base. Before inserting, updating or deleting any object from Data Base with Hibernate, we need to tell Hibernate about our application setting or configuration.

We need to take a example that is, 
           
           What kind of Data Base are we using? 
           How do we connect to the Database?
          What are the object we want to persist?

Now, Here we can explain what is SessionFactory?

SessionFactory is an interface, which is available in “org.hibernate” package.

Session factory is long live multithreaded object.

Usually one session factory should be created for one database.

When you have multiple databases in your application you should create multiple SessionFactory object.

Assume the scenario that you are using one database called mysql in your application then following is the way to create the SessionFactory object.

For configuring the session we need to follow this:

public void testConnection() throws Exception

{

logger.info("Trying to create a test connection with the database.");

Configuration configuration = new Configuration();

configuration.configure("hibernate_sp.cfg.xml");

StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());

SessionFactory sessionFactory = configuration.buildSessionFactory(ssrb.build());

Session session = sessionFactory.openSession();

logger.info("Test connection with the database created successfuly.");

}


In the given code we need to explain it.

When we call to configure() method, then it will automatically search to cfg.xml for Hibernate mapping file.
SessionFactory sc=cfg.buildSessionFactory();

(when we call to this method  SessionFactory object will be created once and it will use by multiple users for long time.)

When we use two multiple database then we need to build 2 SessionFactory object.

Hibernate Programmatic configuration using API:

When we need to configuration Hibernate through programmatic way we use Hibernate provided API to load the hbm.xml files, loat the data base driver and specify the data base connection details.

We can use three way to configure the SessionFactory in Hibernate:

1. Through the HibernateUtil.java file by editing the code in a project.

2. With the help of the XML configuration: XML configuration considered to be the best and standard way to configure hibernate session. By the help of the XML configuration we use to provide database connection parameters, database driver load details, objet to persist details in hibernate.cfg.xml file.

3. Through Properties file configuration: With the help of this type of methods we use the properties file to configure    hibernate   session. It is similar to the XML configuration but usesa properties file. The default name is hibernate.properties.

Read More →