Tuesday, June 11, 2013

03 - Part-1- Writing a Hibernate Application


In this first of a three part - writing a simple Hibernate application from the scratch, we'll learn how to write the Hibernate configuration XML file i.e. hibernate.cfg.xml.


Before we start, we need to know how to store object into database.


Saving Without Hibernate

  • JDBC Database configuration (port number, id, password, driver etc).
  • The Model Object - it is Object which needs to save in DB.
  • Service method to create the model object. We need some method which passes values to the object.
  • Database design - To save the object in DB for which I need a DB table with corresponding columns.
  • DAO method to save the object using SQL queries. It generate the SQL query, it create a connect, run the query and store the data.


The Hibernate Way

  • JDBC Database Configuration - Hibernate configuration. It is a XML file
  • The Model object – Annotations. It is configured in such a way that Hibernate knows how the object stores into DB.
  • Service method to create the model object - Use the Hibernate API.
  • Database design - Not needed! Hibernate creates table itself.
  • DAO method to save the object using SQL queries - Not needed! - Here Hibernate API take care of saving the object.


The default name of Hibernate configuration file is hibernate.cfg.xml, if you strict to this name you will not need to specify Hibernate. If you are using different file name, then you need to pass file name so that hibernate knows where is the configuration file.



hibernate.cfg.xml


<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.driver_class">
org.postgresql.Driver
        </property>
        <property name="connection.url">
jdbc:postgresql://localhost:5433/hibernated
        </property>
        <property name="connection.username">admin</property>
        <property name="connection.password">admin</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">
            org.hibernate.dialect.H2Dialect
        </property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <property name="cache.use_query_cache">true</property>
        <property name="cache.use_second_level_cache">true</property>
        <property name="cache.use_structured_entries">true</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <mapping resource="hibernate-config/domain/Event.hbm.xml"/>
        <mapping resource="hibernate-config/domain/Person.hbm.xml"/>
        <mapping resource="hibernate-config/domain/PhoneNumber.hbm.xml"/>
        <mapping resource="hibernate-config/domain/Account.hbm.xml"/>
        <mapping resource="hibernate-config/domain/HolidayCalendar.hbm.xml"/>
        <mapping resource="hibernate-config/domain/Item.hbm.xml"/>

    </session-factory>

</hibernate-configuration>



Dialect: Databases implement subtle differences in the SQL they use. Things such as data types for example vary across databases (e.g. in Oracle I might put an integer value in a number field and in SQL Server use an int field). Or database specific functionality - selecting the top n rows is different depending on the database. The dialect abstracts this so you don't have to worry about it.



Introduction:

While working with Hibernate web applications we will face so many problems in its performance due to database traffic. That to when the database traffic is very heavy . Actually hibernate is well used just because of its high performance only. So some techniques are necessary to maintain its performance. Hibernate Caching is the best technique to solve this problem. In this article we will discuss about, how we can improve the performance of Hibernate web applications using caching.



The performance of Hibernate web applications is improved using caching by optimizing the database applications. The cache actually stores the data already loaded from the database, so that the traffic between our application and the database will be reduced when the application want to access that data again. Maximum the application will works with the data in the cache only. Whenever some another data is needed, the database will be accessed. Because the time needed to access the database is more when compared with the time needed to access the cache. So obviously the access time and traffic will be reduced between the application and the database. Here the cache stores only the data related to current running application. In order to do that, the cache must be cleared time to time whenever the applications are changing.



1) First-level cache:

First-level cache always Associates with the Session object. Hibernate uses this cache by default. Here, it processes one transaction after another one, means wont process one transaction many times. Mainly it reduces the number of SQL queries it needs to generate within a given transaction. That is instead of updating after every modification done in the transaction, it updates the transaction only at the end of the transaction.



2) Second-level cache:

Second-level cache always associates with the Session Factory object. While running the transactions, in between it loads the objects at the Session Factory level, so that those objects will available to the entire application, don’t bounds to single user. Since the objects are already loaded in the cache, whenever an object is returned by the query, at that time no need to go for a database transaction. In this way the second level cache works. Here we can use query level cache also.



Hibernate supports four open-source cache implementations named 
  1. EHCache (Easy Hibernate Cache) 
  2. OSCache (Open Symphony Cache) 
  3. Swarm Cache and 
  4. JBoss Tree Cache
Each cache has different performance, memory use, and configuration.


MySQL: hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <property name="connection.driver_class">
com.mysql.jdbc.Driver
        </property>
        <property name="connection.url">
            jdbc:mysql://localhost:9909/hibernate
  </property>
        <property name="connection.username">root</property>
        <property name="connection.password"></property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">
  org.hibernate.dialect.MySQLDialect
        </property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>
       
        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>
       
        <!-- Drop and re-create the database schema on startup -->
            <property name="hbm2ddl.auto">create</property> 
   
          <!-- Names the annotated entity class -->
            <mapping class="org.yash.dto.UserDetails"/>    

    </session-factory>

</hibernate-configuration>

No comments:

Post a Comment