1. Hibernate
Introduction and Overview
>By Manisha
May 2012
2. Agenda
At the end of this course, you should be able to:
Describe the basics of Hibernate
Use Hibernate configuration
Explain basic O/R Mapping
Write simple application in Hibernate
●
Q&A
2 Hibernate Basic May 2012
3. Hibernate
●
ORM technology
Consistent database communication and Increased maintainability: In
Hibernate database communication policies are applied as configuration,
in contrast as to code them in SQL based communication.
Developers with limited SQL knowledge can participate in development
Reduced testing time
Performance Optimization: Fine tuning database communication,
adopting caching strategies can be done with configuration.
Portable code across multiple databases: Hibernate API and HQL can
facilitate database neutral programming.
3 Hibernate Basic May 2012
4. What is ORM?
Object Relational Mapping (ORM) frameworks are those which
encapsulate SQL communication with relational databases, with object-
oriented communication.
ORM is a programming way for converting data between incompatible
type systems in relational databases and object-oriented programming
languages.
Currently, several ORM frameworks such as Entity beans in Enterprise
JavaBeans (EJB), TopLink from Oracle, iBatis, Hibernate, and so on are
available.
4 Hibernate Basic May 2012
5. Difference in conventional and ORM based implementation
Conventional Implementation
Service Data
Access Database
Object
Object
Implementation with ORM
Data
Service Hibernate
Access
Object Database
Object
5 Hibernate Basic May 2012
6. High level Hibernate
Architecture
Application
Persistent Objects
Hibernate
Configuration Mapping
Database
6 Hibernate Basic May 2012
7. Basic content of Hibernate application
In any standard hibernate application will have below data
Pojo class of table
Configuration files(hibernate.cfg.xml)
Mapping file(xxx.hbm.xml)
Application code to create Hibernate Runtime and execute
7 Hibernate Basic May 2012
8. How to use Hibernate for data communication?
Follow these steps to use Hibernate for data communication:
Prepare domain objects (POJO) for table.
Write mapping information.
Write Hibernate configuration.
Create Hibernate Runtime.
Use Hibernate Runtime to communicate with database.
8 Hibernate Basic May 2012
9. Hibernate Example: Prepare pojo object(Class to table mapping)
package com.ibm.tf.hibernate;
public class Employee implements Serializable{
// attributes - relate to business entities
private Integer id;
private firstName;
private lastName;
// accessor and mutator methods
public Integer getId() {
//implementation code
}
….
public void printEmployeeName() {
// some business method
}
}
9 Hibernate Basic May 2012
10. Write mapping information(Table mapping to conf. Xml file)
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<!– Employee class mapping to EMPLOYEE table-->
<class name=" com.ibm.tf.hibernate.Employee“ table=“EMPLOYEE">
<!– identifier column mapping-->
<id name="id“ column=“ID“ type="long">
<generator class="native"/>
</id>
<!– property mapping--->
<property name=“firstName“ column=“FIRST_NAME“ type="string"/>
<property name=“lastName“ column=“LAST_NAME“ type="string"/>
</class>
</hibernate-mapping>
10 Hibernate Basic May 2012
12. Write Hibernate configuration information
The following are illustrative (not exhaustive) configuration parameters sorted by their
categories. With category description we can guess on what kind of values could be
assigned to individual parameters.
Hibernate JDBC Properties (useful for standalone)
– hibernate.connection.driver_class
– hibernate.connection.url
– hibernate.connection.username
– hibernate.connection.password
Hibernate Datasource Properties (useful inside container)
– hibernate.connection.datasource
– hibernate.jndi.url
List of managed entities
Hibernate Configuration Properties:
– hibernate.dialect n hibernate.order_updates
12 Hibernate Basic May 2012
13. Create Hibernate runtime
Before creating hibernate runtime, lets understand It
Hibernate Session, Transaction, and all Objects working behind them are
collectively called as Hibernate Runtime.
Information like how to connect to database, how to map entity data to table
row, how to handle transactions and many more are provided to Hibernate
Runtime by means of configuration.
As Hibernate is designed to operate in many different environments. Hence,
there are:
– Many configurable parameters of various category.
– Many ways to configure Hibernate Runtime.
13 Hibernate Basic May 2012
14. Create hibernate runtime
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure()
.buildSessionFactory();
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
14 Hibernate Basic May 2012
15. Use Hibernate Runtime
public class EemployeetManager {
private void createAndStoreEmployee(String firstNm, String lastNm) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Employee theEmp = new Employee();
theEmp.setFirstName(firstNm);
theEmp.setLastName(lastNm);
session.save(theEmployee);
session.getTransaction().commit();
}
@SuppressWarnings("unchecked")
private List<Employee> listEmployee() {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List<Employee> result = session.createQuery("from Eemployee").list();
session.getTransaction().commit();
return result;
}
}
15 Hibernate Basic May 2012
16. Example artifacts in Hibernate Architecture
EmployeeManager.java Application HibernateUtil.java
Persistent Objects
Employee.java
Configuration Hibernate Mapping
Hibernate.cfg.xml Employee.hbm.xm
l
Database
jdbc:hsql:file:testdb
16 Hibernate Basic May 2012
18. Questions and Answers
Choose the correct option:
ORM framework is the one which:
a. Abstracts communication with object-oriented
database.
b. Abstracts communication with Relational
database.
c. Acts as database driver.
d. Is a replacement of Java EE Application
Container.
18 Hibernate Basic May 2012
19. Questions and Answers
Choose the correct option:
We can configure JNDI name for session factory to which
Hibernate can automatically bind its _.
a. SessionFactory
b. JMX deployment
c. hibernate.dialect
19 Hibernate Basic May 2012
20. Questions and Answers
Which of the following is incorrect about <property> element?
a. Maps to attributes of the entity class
b. Can be computed dynamically
c. Column attribute is optional
d. Name attribute can only start with a capital letter
20 Hibernate Basic May 2012
21. Questions and Answers
Choose the correct option:
Hibernate dialects are used in configuration to:
a. Connect to database instance.
b. Replace database driver
c. Make Hibernate generate database specific queries.
21 Hibernate Basic May 2012