Sunday 21 July 2013

Hibernate Configuration

Hibernate Configuration

WE can configure hibernate by adding the .jar(s) files to our java application

  • Required files of Hibernate Application. (Hibernate requires two XML files)
  1. mapping file
  2. configuration file (Hibernate.cfg.xml)
What is Mapping?

Mapping is a mechnism for establishing synchronization between object and row of the table.
Mapping can be done using 2 ways:
  1. XML (Mapping XML file)
  2. Annotations 
  •  
usage of  XML files for mapping are more effective and easy when compared with Annotations.

Syntax: for mapping with XML file:
 

<? xml version="1.0">
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/hibernate Mapping DTD 3.0//EN"
       "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
 <class name="POJO class name " table="table name">
   < id name ="variable name" column=" primary key column name"
     type="java/hibernate type"> <generate class="increment"></generator>
   </id>
  <property name="variable2 name" column="column name  in database"
   type="java/hibernate type"/>
 <property name="variable2 name" column="column name in database" type="java/hibernate type"/>
</class>
</hibernate-mapping>
  • we will include Hibernate DTD for auto complesion of XML mapping elements and attributes in our editor or IDE.
  • The DTD file is included in hibernate-core.jar
<class > element: class element is a child of hibernate-mapping tag. All persistent entity classes need a mapping to a table in the SQL database.

<id> element: Each instance is a row in the table. We need unique identifier property to map as primary key in the table to identify each row uniquely. The id element is the declaration of the identifier property.

<generator>  element: The nested generator element specifies the identifier generation strategy. Hibernate supports different identifier generation strategy algorithm. These strategies depends on configured database dialect.

<property> element:  specifies properties in a class other than id property. The name attribute of the property element tells Hibernate which getter and setter methods to use

configuration file( hibernate.cfg.xml)

configuration file is loaded in to an hibernate application when we run the hibernate application. Configuration file should have 3 kinds of properties.
  1. connection properties
  2. Hibernate properties
  3. Mapping file properties
  • we may have one or more configuration files based on number of databases we are using for getting date into our application.
  • if we want to populate data from two databases like Oracle, MySql then we must create two configuration files. i.e: Number of configuration files in the hibernate application =Number of databases used in application.
<hibernate-configuration>
 <session-factory >
              <!--connection  Properties--->
         <property name="connection.driver class">Driver class name
         </property>
         <property name="connection.url">URL</property>
         <property name="connection.user">user</property>
        <property name="connection.password">password</property>

             <!--END of connection properties-->
             <!--Hibernate properties-->
             <property name="show_sql">true/false</property>
             <property name="dialet">Database dialet class</property>
            <property name="hbm2dd1.auto">create/update or whatever</property>

           <!--END of Hibernate Properties-->

           <!--mapping file names-->
          <mapping resource="hbm file1 name.xml"/>
         <mapping resource="hbm file2 name.xml"/>
         <!--END of Related to the mapping-->
     </session-factory>
</hibernate-configuration>

No comments:

Post a Comment