Hibernate: Why ORM?
Hibernate: Why ORM?
Configuration Object
Session Factory
Session
Transaction
Query
Criteria
Hibernate Configuration & Mapping Xml Documents.
DB Configuration.
Pool Management Properties.
Transaction Properties.
Cache Configuration.
SQL Dialect.
Logging.
3. API style:
The org.hibernate.cfg.Configuration object supports various methods to set the
hibernate config properties such as addClass(), addResource().
Properties File can be used to configure common properties between multiple
databases that we want to access.
The following are few properties supported by hibernate.
1. connection.driver_class specifies the qualified name of the JDBC driver
class used by hibernate to establish the connection to database.
2. connection.url
3. connection.username
4. connection.password
5. show_sql
6. dialect
We want to add hibernate. Prefix to all these properties while describing them in
properties file.
Hibernate Mapping:
We can configure the mapping details in 2 different styles,
1. Xml style
Create the hibernate xml mapping document.
Load the mapping definition into the configuration object.
2. Annotation style.
Implementing Hibernate Persistent Object:
Hibernate supports POJOs as persistent objects, that means, we dont have to use
hibernate API for implementing the persistent object.
However, we have to follow few rules listed below while implementing the
hibernate persistent object.
The persistent properties should be implemented in java bean style, i.e., a
setter and getter method.
Should support no argument constructor.
The persistent object and the properties should be non-final in the case if the
object is configured for lazy loading.
Implements equals and hash code method.
Example 1:
package com.acc.hibernate;
public class Employee
{
private int empId;
private String empName;
private String designation;
//setters & getters
}
Employee.hbm.xml:
<hibernate-mapping>
<class name = com.acc.hibernate.Employee table = emp_tab>
<id name = empId column = empid>
<generator class = assigned/>
</id>
<property name = empName column = ename/>
<property name = designation />
</class>
</hibernate-mapping>
Hibernate.cfg.xml:
<hibernate-configuration>
<session-factory>
<property name
</property>
connection.driver_class>oracle.jdbc.driver.OracleDriver
package com.acc.hibernate;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class HibernateTestCase
{
public static void main(String args[])
{
Configuration conf = new Configuration().configure();
SessionFactory sf = conf.buildSessionFactory();
Session session = sf.openSession();
Employee emp = (Employee)session.load(Employee.class,101);
System.out.println(emp.getEmpId());
System.out.println(emp.getEmpName());
System.out.println(emp.getDesignation());
session.close();
}
}
To run this example set the classpath to the jar files,
hibernate3.jar
log4j.jar
commons-logging.jar
dom4j.jar
cglib.jar
ehcache.jar
jta.jar
ojdbc14.jar
commons-dbcp.jar
commons-collections.jar
commons-pool.jar
Problem of Granularity:
Public class PersonalDetails
{
Private int empNo;
Private String fname,lname,email;
System.out.println(pd.getFname());
System.out.println(pd.getAddr().getStreet());
session.close();
}
}
http://www.vaannila.com/hibernate/hibernate-example/hibernate-mapping-one-toone-1.html
follow this above link to see how one-to-one relationship is established using
hibernate.
<many-to-one>:
The name attribute specifies the property name to represent this relationship. The
column attribute specifies the database column name that is mapped to the identity
of the other end object of this relation.
<one-to-many>:
<set name= emps>
<key column = dno/>
<one-to-many class = Employee/>
</set>
Lazy attribute:
This supports true/false.
Specifying true describes not to load the relationship data when the relationship
property is accessed and false describes to eagerly load the relationship data.
Cascade attribute:
Hibernate supports cascade option to describe about the save, update and delete
operations with respect to the relationship. The following values are supported by
this property,
1. none
This is default taken by hibernate, if this property is not used.
This describes to ignore the relation objects for insert, update and delete
operations.
2. save_update
Specifies to consider the relationship property for insert and update operations
only.
3. Delete
Problem of sub-types:
Employee.hbm.xml:
<hibernate-mapping package = com.acc.hib>
<class name = Employee table = emp_details>
<id name = empNo>
<generator class = increment/>
</id>
<discriminator column = emp_type/>
<property name = name/>
<property name = emailId/>
<property name = mobile/>
<subclass name = SalariedEmployee discriminator-value = salaried>
<property name = salary column = sal/>
</subclass>
<subclass name = HourlyEmployee discriminator-value = hourly>
<property name = max_hours_per_day/>
<property name = rate_per_hour/>
</subclass>
</class>
</hibernate-mapping>
In this case the common and specific data elements in the hierarchy are saved
into a single table however the table could be added with an additional column
that describes about the type of the object the record is representing.
The additional column emp_type that is designed to describe the object type
is referred as discriminator column.