Thursday, March 31, 2016

Session Factory Session and Hibernate.cfg.xml

          
            Let move on next, how to configure hibernate in java code and what is hibernate.cfg.xml and its properties. For the basic Hibernate architecture please look my previous post.


Look the below code, 

public void hibernateConfiguration() {
SessionFactory sessionFactory = null;
Session session = null;
try {
       Configuration configuration = new Configuration();
       configuration.configure("hibernate.cfg.xml");
       ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().
              applySettings(configuration.getProperties()).build();
       sessionFactory = configuration.buildSessionFactory(serviceRegistry);
       session = sessionFactory.openSession();
       persistEmployee(session);
     } catch (Exception e) {
         LOGGER.log(Level.SEVERE, e.getMessage(), e); //Simply your logger message
     } finally {
          if (session != null) {
              session.close();
     }
     if (sessionFactory != null) {
        sessionFactory.close();
    }
  }

               From the above code, hibernateConfiguration() creates a new instance for Configuration class. For configuring hibernate.cfg.xml, you must put configuration xml file at root of your project classpath. Else, you may encountered a problem hibernate.cfg.xml not found. Runtime only this file will be find.


       Pass your hibernate.cfg.xml as a string arguments for the configure method like configuration.configure("hibernate.cfg.xml"); There is lot of others way also to configure this xml means, getting from URL, File location etc., For referring those all methods, please check this link .

            Next, create serviceregistry instance and pass it as an argument to buildSessionFactory() method of configuration. ServiceRegistry is redesigned for some performance purpose and it was introduced in Hibernate 4. For more please visit this ServiceRegistry.

Now SessionFactory can obtain session for to do the activities with Entities.

Difference between SessionFactory and session?

     Simply we can say, sessionFactory is an instance, which will be used to create an instance for sessions.

SessionFactory is thread safe whereas session is not thread safe.

hibernate.cfg.xml explanation



org.h2.Driver
jdbc:h2:~/hibernateTest;AUTOCOMMIT=OFF


1
org.hibernate.dialect.H2Dialect
thread
org.hibernate.cache.internal.NoCacheProvider</property>
true
true
create


Now let’s check all the properties of sessionFactory in above xml.

connection.driver_class:

            Specifies the database driver that should be used for your application. Here I have taken H2 database (In Memory Database).

connection.url :

            It specifies the JDBC URL , here the case is , h2 and database name is :~/hibernateTest and I want to commit data by my own code. So I have chooses AUTOCOMMIT=OFF.

connection.username:  

            Specifies the database user name.

connection.password:

            Specifies the database password.

connection.pool_size:

            Specifies the connection pool size for your application. Here my case, my application will use single thread as execution, So I put 1 as pool size. Suppose, if your application made a dealing with multiple threads and multiple users, then appropriate size should choose. 

dialect:

            Specifies a java class that will translate specific SQL dialect, here the case is H2.

current_session_context_class:

            Specifies the hibernate session configured, default value is JTA(Java Transaction API). Here I ordered hibernate to make a configuration with thread to do the operation with current thread.

cache.provider_class:

            Specifies to utilize an entity cache. I don’t like to use cache here, so chosen NoCacheProvider Cache provider is a big concept, will publish this as a separate post.

show_sql:

            To display the sql in console output.

format_sql:

            Sql query will be formatted for user readability.

hbm2ddl.auto:

            Specifies whether hibernate want to create schema while during start up. I used property value as create for creating tables during start up.

mapping resource:

            mapping a resource, which contains mapping information for your application.

No comments:

Post a Comment