Thursday, 19 January 2017

Hibernate Configuration

Hibernate Configuration

Hibernate requires to know in advance where to find the mapping information that defines how your Java classes relate to the database tables. Hibernate also requires a set of configuration settings related to database and other related parameters. All such information is usually supplied as a standard Java properties file called hibernate.properties, or as an XML file named hibernate.cfg.xml.

Hibernate Properties:

 

S.N.
Properties and Description
1
hibernate.dialect
This property makes Hibernate generate the appropriate SQL for the chosen database.
2
hibernate.connection.driver_class
The JDBC driver class.
3
hibernate.connection.url
The JDBC URL to the database instance.
4
hibernate.connection.username
The database username.
5
hibernate.connection.password
The database password.
6
hibernate.connection.pool_size
Limits the number of connections waiting in the Hibernate database connection pool.
7
hibernate.connection.autocommit
Allows autocommit mode to be used for the JDBC connection.

hibernate.cfg.xml file
<hibernate-configuration>
    <session-factory>
   <property name="hibernate.dialect">
        org.hibernate.dialect.MySQLDialect
   </property>
   <property name="hibernate.connection.driver_class">
       com.mysql.jdbc.Driver
   </property>

   <!-- Assume test is the database name -->
   <property name="hibernate.connection.url">
      jdbc:mysql://localhost/test
   </property>
   <property name="hibernate.connection.username">
      root
   </property>
   <property name="hibernate.connection.password">
      root123
   </property>

   <!-- List of XML mapping files -->
   <mapping resource="Employee.hbm.xml"/>
   </session-factory>
</hibernate-configuration>

Employee.hbm.xml file

<hibernate-mapping>
   <class name="Employee" table="EMPLOYEE">
      <meta attribute="class-description">
         This class contains the employee detail.
      </meta>
      <id name="id" type="int" column="id">
         <generator class="native"/>
      </id>
      <property name="firstName" column="first_name" type="string"/>
      <property name="lastName" column="last_name" type="string"/>
      <property name="salary" column="salary" type="int"/>
   </class>
</hibernate-mapping>

The <generator> element within the id element is used to automatically generate the primary key values. class attribute of the generator element is set to native to let hibernate pick up either identity, sequence or hilo algorithm to create primary key depending upon the capabilities of the underlying database.
Association Mappings:
The mapping of associations between entity classes and the relationships between tables is the soul of ORM. Following are the four ways in which the relationship b/w the objects can be expressed.
Mapping type
Description
many-to-one association is the most common kind of association where an Object can be associated with multiple objects. For example a same address object can be associated with multiple employee objects
<many-to-one name="address" column="address"  class="Address" not-null="true"/>
one-to-one association is similar to many-to-one association with a difference that the column will be set as unique. For example an address object can be associated with a single employee object
<many-to-one name="address" column="address" unique="true"        class="Address" not-null="true"/>
One-to-Many mapping can be implemented using a Set java collection that does not contain any duplicate element
<set name="certificates" cascade="all">
         <key column="employee_id"/>
         <one-to-many class="Certificate"/>
 </set>

Exp: Each Department can be associated with multiple Employees and each Employee can have only one Department.
Many-to-Many mapping can be implemented using a Set java collection that does not contain any duplicate element
<set name="certificates" cascade="save-update" table="EMP_CERT">
         <key column="employee_id"/>
         <many-to-many column="certificate_id" class="Certificate"/>
 </set>

Exp: Each Employee can attain more than one meetings and each meetings can have more than one employee

Component Mappings:
It is very much possible that an Entity class can have a reference to another class as a member variable. If the referred class does not have its own life cycle and completely depends on the life cycle of the owning entity class, then the referred class is called as the Component class.

No comments:

Post a Comment