SlideShare a Scribd company logo
Object Persistence using Hibernate
An Object-Relational mapping framework
for object persistence
What Hibernate Does
 Object - Relational mapping
 Transparent persistence & retrieval of objects
 Persistence of associations and collections
 Guaranteed uniqueness of an object (within a session)
Hibernate Features
 O-R mapping using ordinary JavaBeans
 Can set attributes using private fields or private setter
methods
 Lazy instantiation of collections (configurable)
 Polymorphic queries, object-oriented query language
 Cascading persist & retrieve for associations, including
collections and many-to-many
 Transaction management with rollback
 Can integrate with other container-provided services
Application Architecture
User Interface
Application Logic
Domain Objects DAO
Hibernate
JDBC
Foundation Classes
UI event
data request
Hibernate API domain object
domain object
data xfer object
JDBC API ResultSet, etc.
hibernate.cfg.xml
*.hbm.xml class mappings
SessionFactory
Another View
Source: Hibernate Reference Manual (online)
Example of Persisting an Object
// get a Hibernate SessionFactory for Session management
sessionFactory = new Configuration()
.configure().buildSessionFactory();
// an Event object that we want to save
Location ku = new Location( "Kasetsart University" );
ku.setAddress( "90 Pahonyotin Road; Bangkok" );
Event event = new Event("Java Days");
event.setLocation( ku );
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
session.save( event );
tx.commit();
session.close();
Example of Retrieving an Object
// use the existing session factory
Session session = sessionFactory.openSession();
// query Event objects for "Java Days"
Transaction tx = session.beginTransaction();
Query query = session.createQuery(
"from Event where name='Java Days'" );
List events = query.list( );
out.println("Upcoming Java Days events: ");
for( Object obj : events ) {
Event event = (Event) obj;
String name = event.getName( );
Location loc = event.getLocation( );
...
}
tx.commit( );
Using Named Parameters in Query
// use the existing session factory
Session session = sessionFactory.openSession();
// Hibernate Query Language (HQL) can use named params
Query query = session.createQuery(
"from Event where name=:name");
query.setParameter( "name", "Java Days");
List events = query.list( );
out.println("Upcoming Java Days events: ");
for( Object obj : events ) {
Event event = (Event) obj;
String name = event.getName( );
Location loc = event.getLocation( );
...
Exercise 1
 Configure a project with Hibernate
 create an EventManager project
 add Hibernate libraries to the project
 add JAR for database driver
 create a hibernate.cfg.xml file
 create log4j.properties
Project Configuration for Hibernate
EventManager/ the project base directory
src/
hibernate.cfg.xml Hibernate configuration file
log4j.properties Log4J configuration file
eventmgr/ base package is "eventmgr"
domain/
Location.java
location.hbm.xml O-R mapping file for a class
build/
hibernate.cfg.xml copied here by IDE during build
log4j.properties copied here by IDE during build
eventmgr/
domain/
Location.class
location.hbm.xml copied here by IDE during build
lib/
hibernate3.jar Hibernate requires several jars
asm.jar
...
Where to Get Hibernate
http://www.hibernate.org Local copy:
http://se.cpe.ku.ac.th/download/hibernate
Required Hibernate Libraries
 Libraries are in lib/ directory
of Hibernate distribution.
 Which ones do you need?
 See _README.txt or my
Using Hibernate notes.
 In your IDE:
 create a Hibernate "library"
 add JARs to the library
 better than copying JAR
files to your project
Required Libraries
hibernate3.jar
antlr-2.7.6.jar
asm.jar
cglib.jar
commons-collections.jar
commons-logging.jar
dom4j.jar
ehcache.jar
jta.jar
log4j.jar
// and maybe
xml-apis.jar
Create a Hibernate Library in Eclipse
1. Project -> Properties -> Java Build Path
2. select Libraries tab.
3. Click "Add Library..." and select "User Library", Next>
4. Create a new user library named "Hibernate"
5. Add Jar files
Add Library or jar for Database Driver
 For Embedded Derby Database
/path-to-derby/lib/derby.jar
 For HSQLDB
/path-to-hsqldb/lib/hsqldb.jar
hibernate.cfg.xml for Embedded Derby DB
<?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="dialect">
org.hibernate.dialect.DerbyDialect</property>
<property name="connection.driver_class">
org.apache.derby.jdbc.EmbeddedDriver</property>
<property name="connection.url">
jdbc:derby:/temp/eventmgr;create=true</property>
<property name="hbm2ddl.auto">create</property>
<!-- O-R mappings for persistent classes -->
<mapping resource="eventmgr/domain/Location.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Remove after database created
hibernate.cfg.xml for MySQL
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC ... remainder omitted >
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect </property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver </property>
<property name="connection.username">student</property>
<property name="connection.password">secret </property>
<property name="connection.url">
jdbc:mysql://se.cpe.ku.ac.th/eventmgr </property>
<!-- Object-Relational mappings for classes -->
<mapping resource="eventmgr/domain/Location.hbm.xml"/>
... other mappings omitted
</session-factory>
</hibernate-configuration>
XML Syntax, 1
 XML Declaration: "this file contains XML"
<?xml version='1.0' encoding='utf-8'?>
 Root Element: "this whole file is a hibernate-configuration"
<hibernate-configuration>
content of document goes here
<hibernate-configuration>
 Child Elements: define content, have a tree-like nested structure
<session-factory>
...
</session-factory>
 Tree structure & Scope: all elements must have a closing tag
<class name="Event" table="EVENTS"> a start element
<property name="location" /> element start & end
</class> an end tag
XML Syntax, 2
 Attributes: values must always be quotes, no duplicates
<class name="Event" table="EVENTS">
 Special characters: < > & ' " must use reference except in CDATA
<message>
&quot;Hello &lt;b&gt;world&lt;/b&gt;&quot
</message>
 Child Elements can be repeated (depends on DTD)
<courselist name="courses">
<course>object oriented programming</course>
<course>software spec &amp; design</course>
</courselist>
 Elements can sometimes be written as attributes
<course>
<id>219341</id> <course id="219341"/>
</course>
Logging
 Hibernate apps will log errors and/or activity.
 Two choices:
 Java SDK logging (since JDK 1.4)
 Log4j
 if you use Log4j, create a log4j.properties in
your application source root directory.
 Copy this file from the Hibernate etc/ directory.
Sample log4j.properties
 Too long to print here
 See an actual file, for example:
[hibernate.dir]/doc/tutorial/src/log4j.properties
 Configuration logging of Hibernate:
log4j.logger.org.hibernate=warn
 Log the SQL generated by Hibernate:
#log4j.logger.org.hibernate.SQL=debug
 Log JDBC bind parameters (can be security leak):
log4j.logger.org.hibernate.type=info
Exercise 2
 Define a Location class and LOCATIONS table
 Write the class
 Create a mapping file for Location: Location.hbm.xml
Create the Location class
Location class
Location
id: int
name: String
address: String
LOCATIONS
PK id INTEGER
name VARCHAR(80)
address VARCHAR(160)
LOCATIONS table (Hibernate can auto-generate this)
Write the Location class
package eventmgr.domain;
public class Location {
private int id;
private String name;
private String address;
public Location() { } // a no argument constructor
public int getId( ) { return id; }
public void setId( int id ) { this.id = id; }
public String getName( ) { return name; }
public void setName( String n ) { this.name = n; }
public String getAddress( ) { return address; }
public void setAddress( String a ) { address = a; }
}
Use JavaBean conventions
in Persistent object classes.
Hibernate can access private methods
package eventmgr.domain;
public class Location {
private int id;
private String name;
private String address;
public Location() { }
public int getId( ) { return id; }
private void setId( int id ) { this.id = id; }
public String getName( ) { return name; }
private void setName( String n ) { this.name = n; }
public String getAddress( ) { return address; }
private void setAddress( String a ) { address = a; }
}
OK to use "private" or
"protected" for mutators.
Hibernate can access private data, too
public class Location {
private int id;
private String name;
private void setName( String name ) {
if ( name.length() < 3 )
new RuntimeException("name too short");
...
} Some mutator methods contain data validity
checks or other complicated logic.
to tell Hibernate to set the field values directly (don't use the "set"
method) in the class mapping file write:
<hibernate-mapping default-access="field">
...
Schema to create Locations table
CREATE TABLE locations (
id INTEGER NOT NULL,
name VARCHAR(80) NOT NULL,
address VARCHAR(160),
PRIMARY KEY(id)
) DEFAULT CHARSET=utf8 ;
This works for MySQL.
Hibernate can generate table schema at runtime.
mysql> use eventmgr ;
mysql> source tableschema.sql ;
O-R Mapping for the Location class
Map between object attributes and table columns.
LOCATIONS
PK id INTEGER
name VARCHAR(80)
address VARCHAR(160)
Location
id: int
name: String
address: String
O-R Mapping requires a mapping file
Mapping File Location.hbm.xml
An XML file describing how to map object to table.
Filename: Location.hbm.xml
<!DOCTYPE hibernate-mapping PUBLIC ... remainder omitted >
<hibernate-mapping package="eventmgr.domain">
<class name="Location" table="LOCATIONS">
<id name="id" column="id">
<!-- let hibernate choose id for new entities -->
<generator class="native"/>
</id>
<property name="name" column="name" not-null="true"/>
<property name="address"/>
</class>
</hibernate-mapping>
Mapping File Explained
<hibernate-mapping>
<class name="eventmgr.domain.Location"
table="LOCATIONS"
options... >
<id name="id"
column="id"
unsaved-value="0">
<generator class="native"/>
</id>
object attribute mappings
</class>
</hibernate-mapping>
root element of a hibernate mapping
mapping for one class
Every persistent class needs an "identifier" attribute and column.
The identifier is used to establish object identity (obj1 == obj2) and locate the
table row for a persisted object. The id is usually the Primary Key of the table.
Attribute Mapping: <property .../>
<property name="name"
column="name“
type=“name“
not-null="true“
/>
You omit elements if Hibernate can guess the value itself:
<property name="address" column="ADDRESS" type="string"/>
<!-- omit data type and Hibernate will determine it -->
<property name="address" column="ADDRESS"/>
<!-- omit column if same as attribute (ignoring case)-->
<property name="address"/>
Name of the attribute in Java class
Column name in the
database table
Hibernate or Java data
type (usually you can
omit it)
Constraints
What you have so far
 Project configured for Hibernate
 Hibernate configuration file
 Location class and mapping file Location.hbm.xml
 Configure a database schema
(for learning, we will auto-generate schema)
HibernateUtil: a Session Factory (1)
HibernateUtil
-sessionFactory: SessionFactory
+getSessionFactory( ): SessionFactory
+getCurrentSession( ): Session
+openSession( ): Session
// always use the same sessionFactory (Singleton)
SessionFactory sessionFactory = new Configuration()
.configure().buildSessionFactory();
// use sessionFactory to get a Session
Session session = sessionFactory.openSession();
// or the more convenient:
Session session = sessionFactory.getCurrentSession();
1
HibernateUtil: a Session Factory (2)
public class HibernateUtil {
private static SessionFactory sessionFactory = null;
private static final Logger log = Logger.getLogger(..);
public static SessionFactory getSessionFactory() {
if ( sessionFactory == null ) {
try { // Create the SessionFactory
sessionFactory = new Configuration()
.configure().buildSessionFactory();
} catch (Exception e) {
System.err.println("sessionFactory error "+ ex);
log.error("SessionFactory creation failed", ex);
//throw new ExceptionInInitializerError(ex);
}
}
return sessionFactory;
HibernateUtil: a Session Factory (3)
/**
* Get the current Hibernate session.
* This creates a new session if no current session.
* @return the current Hibernate Session
*/
public static Session getCurrentSession() {
return getSessionFactory().getCurrentSession();
}
public static Session openSession() {
return getSessionFactory().openSession();
}
Persistence Test: LocationTest.java
public class TestLocation {
public static void testSave() {
Location loc1 = new Location( );
loc1.setName("Kasetsart University");
loc1.setAddress("90 Pahonyotin Rd, Bangkok");
Location loc2 = new Location();
loc2.setName("UCLA");
loc2.setAddress("Westwood, California");
Session session =
HibernateUtil.getCurrentSession();
Transaction tx = session.beginTransaction();
session.saveOrUpdate( loc1 );
session.saveOrUpdate( loc2 );
tx.commit();
System.out.println("Locations saved");
}
Persistence Test: LocationTest.java
public static void testQuery() {
System.out.println("Retrieving locations");
Session session = HibernateUtil.getCurrentSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery("from Location");
// query.list() returns objects, cast to List<Location>
List<Location> list = (List<Location>)query.list( );
tx.commit();
for(Location l : list ) out.printf("%d %s %sn",
l.getId(), l.getName(), l.getAddress() );
if ( session.isOpen() ) session.close();
}
public static void main( String [] args) {
testSave( ); testQuery( );
Exercise 3
 Test object uniqueness:
 In one session get the same location two times and
compare using (ku1 == ku2). Are they same?
 In different sessions get the same location and
compare (ku1 == ku2). Are they same?
 Test transparent persistence:
 Modify a location inside a session. Does database
change?
 Reattach a modified object to new session. Does
database change?
Getting a Unique Result
Session session = HibernateUtil.getCurrentSession();
Query query = session.createQuery(
"from Location where name=‘Kasetsart University’");
// query.uniqueResult() returns only first match
Location ku1 = (Location) query.uniqueResult( );
Session session = HibernateUtil.getCurrentSession();
Query query = session.createQuery(
"from Location l where l.name like 'Kasetsart%'");
// query.uniqueResult() returns only first match
Location ku1 = (Location) query.uniqueResult( );
Pattern matches "like" - use "%" as wildcard character
Mapping a Class with Associations
Simplified version of Event class.
Event
id: long
name: String
startDate: Date
location: Location
Location
id: int
name: String
address: String
1
*
O-R Mapping of n-to-1 Associations
Event
id: long
name: String
startDate: Date
location: Location
LOCATIONS
PK id INTEGER
name VARCHAR
address VARCHAR
Location
id: int
name: String
address: String
EVENTS
PK id BIGINT
name VARCHAR
start_date TIMESTAMP
FK location_id INTEGER
The Data Mapper converts a n-to-1
association to a foreign key relation (persist)
or foreign key to object (retrieve).
1
*
Mapping for Event (Event.hbm.xml)
Use <many-to-one name="attribute" ... />
to map a reference to another object.
<!DOCTYPE hibernate-mapping PUBLIC ... remainder omitted >
<hibernate-mapping package="eventmgr.domain">
<class name="Event" table="EVENTS">
...
<property name="startDate" column="start_date"
type="timestamp" />
<many-to-one name="location" column="location_id"
class="Location" />
</class>
</hibernate-mapping>
you can omit class (Hibernate can determine itself)
Test: Save an Event
public static void testSave() {
Location loc1 = new Location( );
loc1.setName("Kasetsart University");
loc1.setAddress("90 Pahonyotin Rd, Bangkok");
Event event = new Event( );
event.setName("Java Days");
event.setLocation( loc1 );
event.setStartDate( new Date(108,Calendar.JULY, 1) );
Session session = HibernateUtil.getCurrentSession();
Transaction tx = session.beginTransaction();
session.saveOrUpdate( event );
tx.commit();
System.out.println("Event saved");
}
Did you get an Error?
The Location doesn't exist in database (transient object).
Exception in thread "main“
org.hibernate.TransientObjectException:
object references an unsaved transient instance
- save the transient instance before flushing:
eventmgr.domain.Location
Persisting the Event location
Solutions:
1. save location during the transaction (manual save)
2. tell Hibernate to cascade the save operation
(automatically save Location)
<many-to-one name="location" column="location_id"
class="Location"
cascade="save-update" />
cascade="none" don't cascade operations
"all" cascade all operations (be careful)
"save-update" cascade save and updates
"delete-orphan" cascade all, delete unreferenced orphan children
Test: Retrieve an Event
public static void testRetrieve() {
System.out.println("Retrieving event");
Session session = HibernateUtil.getCurrentSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery(
"from Event e where e.name= :name");
query.setParameter("name", "Java Days");
// query.list() returns objects, cast to List<Location>
List<Event> list = (List<Event>)query.list( );
tx.commit();
for(Event e : list ) out.printf("%d %s %sn",
e.getId(), e.getName(), e.getLocation().getName()
);
}
Lazy Instances and Proxies
Transaction tx = session.beginTransaction();
Query query = session.createQuery(
"from Event e where e.name=:name");
query.setParameter("name", "Java Days");
List<Event> list = (List<Event>)query.list( );
tx.commit();
for(Event e : list ) out.printf("%d %s %sn",
e.getId(), e.getName(), e.getLocation().getName()
);
Error: LazyInstantiationException
• Hibernate uses lazy instantiation and proxy objects
• Hibernate instantiates the location object when it is first accessed
• We closed the transaction before accessing the location
Two Solutions
1. Modify our code: getLocation( ) before closing the
session.
2. Tell Hibernate not to use lazy instantiation of Location
objects (in Location.hbm.xml)
List<Event> list = (List<Event>)query.list( );
for(Event e : list ) out.printf("%d %s %sn",
e.getId(), e.getName(), e.getLocation().getName()
);
tx.commit( );
<class name="Location" table="LOCATIONS" lazy="false">
...
</class>
Creating a Component for Address
Address
street: String
city: String
province: String
Location
id: int
name: String
address: Address
1
*
LOCATIONS
id name address
101 Kasetsart University street city province
102 Seacon Square
Organizing your Work
Use DAO to separate OR behavior from the
rest of your project.
A DAO for the Location class
The "useful" methods depend on the domain class and
the application.
LocationDao
findById(id: int) : Location
findByName(name : String): Location[*]
find(query: String) : Location[*]
save(loc: Location) : boolean
delete(loc: Location) : boolean
Java code for Simple LocationDao
package eventmgr.dao;
import ...;
public class SimpleLocationDao {
private static final Logger logger =
Logger.getLogger(LocationDao.class);
public LocationDao() { }
public Location findById( int id )
public List<Location> findByName( String name )
public List<Location> find( String query )
public boolean save( Location loc )
public boolean delete( Location loc )
}
The core of findById( ) - use "load"
public Location findById( int id ) {
Location result = null;
Session session = null;
Transaction tx = null;
try {
session = HibernateUtil.getCurrentSession();
tx = session.beginTransaction();
result =
(Location)session.load(Location.class, id);
tx.commit();
session.close( );
} catch ...
}
return result;
}
The details of findById( )
public Location findById( int id ) {
Location result = null;
Session session = null;
Transaction tx = null;
try {
session = HibernateUtil.getCurrentSession();
tx = session.beginTransaction();
result = (Location)session.load(Location.class,id);
tx.commit();
} catch (ObjectNotFoundException e) {
logger.info("Object not found. id = "+id, e);
if ( tx != null && ! tx.wasCommitted() ) tx.rollback();
} catch (HibernateException e) {
logger.error("Hibernate exception", e);
if ( tx != null && ! tx.wasCommitted() ) tx.rollback();
} finally {
if ( session != null && session.isOpen() ) session.close();
}
return result;
}
The core of findByName( ) - "query"
public List<Location> findByName( String name ) {
List<Location> result;
...
try {
session = HibernateUtil.getCurrentSession();
tx = session.beginTransaction();
Query query = session.createQuery(
"from Location where name=:name" );
query.setParameter( "name", name );
result = (List<Location>) query.list( );
tx.commit();
session.close( );
} catch ...
return result;
}
Details of findByName( )
 Exercise
The core of save( ) - "saveOrUpdate"
public boolean save( Location location ) {
boolean result = false;
...
try {
session = HibernateUtil.getCurrentSession();
tx = session.beginTransaction();
session.saveOrUpdate( location );
tx.commit();
session.close( );
result = true;
} catch ...
return result;
}
Details of save
 Exercise
The core of delete( ) - "delete"
public boolean delete( Location location ) {
boolean result = false;
...
try {
session = HibernateUtil.getCurrentSession();
tx = session.beginTransaction();
session.delete( location );
tx.commit();
session.close( );
result = true;
} catch ...
return result;
}
Redundant Code
 Every DAO method has the same boilerplate code:
Session session = null;
Transaction tx = null;
try {
session = HibernateUtil.getCurrentSession();
tx = session.beginTransaction();
do the work here
tx.commit();
} catch (ObjectNotFoundException e) {
logger.info("Object not found. "+id, e);
if ( tx != null && ! tx.wasCommitted() ) tx.rollback();
} catch (HibernateException e) {
logger.error("Hibernate exception", e);
if ( tx != null && ! tx.wasCommitted() ) tx.rollback();
} finally {
if ( session != null && session.isOpen() ) session.close();
}
Factor out Redundant Code
Class SimpleLocationDao {
private Session session; // declare as attributes
private Transaction tx;
public Location findById( int id ) {
try {
beginTransaction( );
do the work here
commitTransaction( );
} catch (ObjectNotFoundException e) {
handleError( e );
} catch (HibernateException e) {
handleError( e );
} finally {
if ( session != null && session.isOpen() )
session.close();
}
Duplicate Code Between DAO
 In every DAO, the CRUD methods are almost the same
 Consider save( ) ...
public boolean save( Location location ) {
boolean result = false;
...
try {
beginTransaction( );
session.saveOrUpdate( location );
commitTransaction( );
} catch ( ... ) {
handleException( e );
} finally { ... }
return result;
}
Apply the Layer Superclass Pattern
AbstractDao
#load( class, id ) : Object
#find( class, query ) : List
#saveOrUpdate( object: Object )
#delete( object: Object )
LocationDao EventDao SpeakerDao
AbstractDao.save
protected Object save( Object obj ) {
Object result = null;
try {
beginTransaction();
result = session.saveOrUpdate( obj );
commitTransaction();
} catch (ObjectNotFoundException e) {
handleError( e );
} catch (HibernateException e) {
handleError( e );
} finally {
closeSession( );
}
return result;
}
LocationDao using Layer Superclass
public class LocationDao extends AbstractDao {
public boolean save( Location location ) {
return super.save( location );
}
AbstractDao.load
protected Object load( Class clazz,
Serializable id ) {
Object result = null;
try {
beginTransaction();
result = session.load( clazz, id);
commitTransaction()
} catch (ObjectNotFoundException e) {
handleError( e );
} catch (HibernateException e) {
handleError( e );
} finally {
closeSession( );
}
return result;
}
LocationDao using Layer Superclass
public class LocationDao extends AbstractDao {
public LocationDao( ) { super( ); }
public Location findById( int id ) {
return (Location) super.load(
Location.class, id );
}
Exercise
 use your SimpleLocationDao to write a layer
superclass named AbstractDao.
 write a LocationDao that extends AbstractDao
Hibernate Query Language (HQL)
Query query = session.createQuery(
"from Event where name='Java Days'");
Query query = session.createQuery(
"select e from Event e where e.name='Java Days'");
 Hibernate queries you Hibernate Query Language (HQL).
 HQL is object-centric - use class and property names, not SQL
table names.
HQL example
String query = "select e from Event e where
e.location.name = 'Kasetsart University'";
Query q = session.createQuery( query );
String sqlquery = "select * from EVENTS e
join LOCATIONS l ON e.location_id = l.id
where l.name = 'Kasetsart University'";
Statement stmt = connection.createStatement( sqlquery );
Problem: Find all events which are held at Kasetsart
HQL:
SQL and JDBC:
Many-to-one Associations
public class Event {
private long id;
private String name;
private Location location; // 1-to-many assoc.
private Date startDate;
private long duration;
public Event( ) { }
...
}
Event.hbm.xml Mapping File
<!DOCTYPE hibernate-mapping PUBLIC ... remainder omitted >
<hibernate-mapping package="eventmgr.domain">
<class name="Event" table="EVENTS">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name" column="name" not-null="true"/>
<property name="startDate" column="start_date"
column="timestamp"/>
<property name="duraction" />
<many-to-one name="location" column="location_id"
class="Location"/>
</class>
</hibernate-mapping>
Cascading Save & Update
<class name="Event" table="EVENTS">
<id name="id" column="id">
<generator class="native"/>
</id>
...
<many-to-one name="location" column="location_id"
class="Location"
cascade="save-update"/>
</class>
When you save an Event, should Hibernate automatically
save the location, too?
• no: then you must save the location yourself
• Yes: specify cascade = "save-update"
Other choices for cascade: all, none, save-update, delete
Learning Hibernate
 Tutorial at www.hibernate.org
 Another good tutorial:
http://www.allapplabs.com/hibernate/hibernate_tutorials
.htm
 Peak & Heudecker, Hibernate Quickly, 2006.
 Baur & King, Java Persistence with Hibernate, 2007,
update of Hibernate in Action, and much longer.
 Baur & King, Hibernate in Action, 2005, considered the
best Hibernate book, by one of the Hibernate creators.
Hibernate Tools
Hibernate Tools Eclipse plugin - HibernateConsole and
more
 Test HQL queries, browse database
Middlegen - generates class mapping files (hbm.xml)
from an existing database schema. Has Eclipse plugin.
Hibernate Synchronizer - generates class mapping files
(hbm.xml) and Java classes from a database schema.
hbm2ddl - creates a database schema (in SQL) from a
hbm.xml file.
hbm2java - creates a Java class from a hbm.xml file.

More Related Content

Similar to 5-Hibernate.ppt

Hibernate presentation
Hibernate presentationHibernate presentation
Hibernate presentation
Manav Prasad
 
DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS.ppt
DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS.pptDESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS.ppt
DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS.ppt
AntoJoseph36
 
hibernate-presentation-1196607644547952-4.pdf
hibernate-presentation-1196607644547952-4.pdfhibernate-presentation-1196607644547952-4.pdf
hibernate-presentation-1196607644547952-4.pdf
Mytrux1
 
Hibernate Tutorial
Hibernate TutorialHibernate Tutorial
Hibernate Tutorial
Syed Shahul
 
Dropwizard
DropwizardDropwizard
Dropwizard
Scott Leberknight
 
Play 2.0
Play 2.0Play 2.0
Play 2.0
elizhender
 
Softshake - Offline applications
Softshake - Offline applicationsSoftshake - Offline applications
Softshake - Offline applications
jeromevdl
 
Devoxx08 - Nuxeo Core, JCR 2, CMIS
Devoxx08 - Nuxeo Core, JCR 2, CMIS Devoxx08 - Nuxeo Core, JCR 2, CMIS
Devoxx08 - Nuxeo Core, JCR 2, CMIS
Nuxeo
 
DWR, Hibernate and Dojo.E - A Tutorial
DWR, Hibernate and Dojo.E - A TutorialDWR, Hibernate and Dojo.E - A Tutorial
DWR, Hibernate and Dojo.E - A Tutorial
jbarciauskas
 
Patni Hibernate
Patni   HibernatePatni   Hibernate
Patni Hibernate
patinijava
 
Architecture Components In Real Life Season 2
Architecture Components In Real Life Season 2Architecture Components In Real Life Season 2
Architecture Components In Real Life Season 2
Somkiat Khitwongwattana
 
Hibernate
HibernateHibernate
Hibernate
Prasadvarada V
 
Writing Swift code with great testability
Writing Swift code with great testabilityWriting Swift code with great testability
Writing Swift code with great testability
John Sundell
 
Local data storage for mobile apps
Local data storage for mobile appsLocal data storage for mobile apps
Local data storage for mobile apps
Ivano Malavolta
 
The Past Year in Spring for Apache Geode
The Past Year in Spring for Apache GeodeThe Past Year in Spring for Apache Geode
The Past Year in Spring for Apache Geode
VMware Tanzu
 
Advanced Hibernate Notes
Advanced Hibernate NotesAdvanced Hibernate Notes
Advanced Hibernate Notes
Kaniska Mandal
 
Dependency injection in Drupal 8
Dependency injection in Drupal 8Dependency injection in Drupal 8
Dependency injection in Drupal 8
Alexei Gorobets
 
Hibernate - Part 2
Hibernate - Part 2 Hibernate - Part 2
Hibernate - Part 2
Hitesh-Java
 
Symfony2 from the Trenches
Symfony2 from the TrenchesSymfony2 from the Trenches
Symfony2 from the Trenches
Jonathan Wage
 
Java Configuration Deep Dive with Spring
Java Configuration Deep Dive with SpringJava Configuration Deep Dive with Spring
Java Configuration Deep Dive with Spring
Joshua Long
 

Similar to 5-Hibernate.ppt (20)

Hibernate presentation
Hibernate presentationHibernate presentation
Hibernate presentation
 
DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS.ppt
DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS.pptDESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS.ppt
DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS.ppt
 
hibernate-presentation-1196607644547952-4.pdf
hibernate-presentation-1196607644547952-4.pdfhibernate-presentation-1196607644547952-4.pdf
hibernate-presentation-1196607644547952-4.pdf
 
Hibernate Tutorial
Hibernate TutorialHibernate Tutorial
Hibernate Tutorial
 
Dropwizard
DropwizardDropwizard
Dropwizard
 
Play 2.0
Play 2.0Play 2.0
Play 2.0
 
Softshake - Offline applications
Softshake - Offline applicationsSoftshake - Offline applications
Softshake - Offline applications
 
Devoxx08 - Nuxeo Core, JCR 2, CMIS
Devoxx08 - Nuxeo Core, JCR 2, CMIS Devoxx08 - Nuxeo Core, JCR 2, CMIS
Devoxx08 - Nuxeo Core, JCR 2, CMIS
 
DWR, Hibernate and Dojo.E - A Tutorial
DWR, Hibernate and Dojo.E - A TutorialDWR, Hibernate and Dojo.E - A Tutorial
DWR, Hibernate and Dojo.E - A Tutorial
 
Patni Hibernate
Patni   HibernatePatni   Hibernate
Patni Hibernate
 
Architecture Components In Real Life Season 2
Architecture Components In Real Life Season 2Architecture Components In Real Life Season 2
Architecture Components In Real Life Season 2
 
Hibernate
HibernateHibernate
Hibernate
 
Writing Swift code with great testability
Writing Swift code with great testabilityWriting Swift code with great testability
Writing Swift code with great testability
 
Local data storage for mobile apps
Local data storage for mobile appsLocal data storage for mobile apps
Local data storage for mobile apps
 
The Past Year in Spring for Apache Geode
The Past Year in Spring for Apache GeodeThe Past Year in Spring for Apache Geode
The Past Year in Spring for Apache Geode
 
Advanced Hibernate Notes
Advanced Hibernate NotesAdvanced Hibernate Notes
Advanced Hibernate Notes
 
Dependency injection in Drupal 8
Dependency injection in Drupal 8Dependency injection in Drupal 8
Dependency injection in Drupal 8
 
Hibernate - Part 2
Hibernate - Part 2 Hibernate - Part 2
Hibernate - Part 2
 
Symfony2 from the Trenches
Symfony2 from the TrenchesSymfony2 from the Trenches
Symfony2 from the Trenches
 
Java Configuration Deep Dive with Spring
Java Configuration Deep Dive with SpringJava Configuration Deep Dive with Spring
Java Configuration Deep Dive with Spring
 

Recently uploaded

Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
Nguyen Thanh Tu Collection
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
paigestewart1632
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
iammrhaywood
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching AptitudeUGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
S. Raj Kumar
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
TechSoup
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
HajraNaeem15
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
imrankhan141184
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 

Recently uploaded (20)

Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching AptitudeUGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 

5-Hibernate.ppt

  • 1. Object Persistence using Hibernate An Object-Relational mapping framework for object persistence
  • 2. What Hibernate Does  Object - Relational mapping  Transparent persistence & retrieval of objects  Persistence of associations and collections  Guaranteed uniqueness of an object (within a session)
  • 3. Hibernate Features  O-R mapping using ordinary JavaBeans  Can set attributes using private fields or private setter methods  Lazy instantiation of collections (configurable)  Polymorphic queries, object-oriented query language  Cascading persist & retrieve for associations, including collections and many-to-many  Transaction management with rollback  Can integrate with other container-provided services
  • 4. Application Architecture User Interface Application Logic Domain Objects DAO Hibernate JDBC Foundation Classes UI event data request Hibernate API domain object domain object data xfer object JDBC API ResultSet, etc. hibernate.cfg.xml *.hbm.xml class mappings SessionFactory
  • 5. Another View Source: Hibernate Reference Manual (online)
  • 6. Example of Persisting an Object // get a Hibernate SessionFactory for Session management sessionFactory = new Configuration() .configure().buildSessionFactory(); // an Event object that we want to save Location ku = new Location( "Kasetsart University" ); ku.setAddress( "90 Pahonyotin Road; Bangkok" ); Event event = new Event("Java Days"); event.setLocation( ku ); Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); session.save( event ); tx.commit(); session.close();
  • 7. Example of Retrieving an Object // use the existing session factory Session session = sessionFactory.openSession(); // query Event objects for "Java Days" Transaction tx = session.beginTransaction(); Query query = session.createQuery( "from Event where name='Java Days'" ); List events = query.list( ); out.println("Upcoming Java Days events: "); for( Object obj : events ) { Event event = (Event) obj; String name = event.getName( ); Location loc = event.getLocation( ); ... } tx.commit( );
  • 8. Using Named Parameters in Query // use the existing session factory Session session = sessionFactory.openSession(); // Hibernate Query Language (HQL) can use named params Query query = session.createQuery( "from Event where name=:name"); query.setParameter( "name", "Java Days"); List events = query.list( ); out.println("Upcoming Java Days events: "); for( Object obj : events ) { Event event = (Event) obj; String name = event.getName( ); Location loc = event.getLocation( ); ...
  • 9. Exercise 1  Configure a project with Hibernate  create an EventManager project  add Hibernate libraries to the project  add JAR for database driver  create a hibernate.cfg.xml file  create log4j.properties
  • 10. Project Configuration for Hibernate EventManager/ the project base directory src/ hibernate.cfg.xml Hibernate configuration file log4j.properties Log4J configuration file eventmgr/ base package is "eventmgr" domain/ Location.java location.hbm.xml O-R mapping file for a class build/ hibernate.cfg.xml copied here by IDE during build log4j.properties copied here by IDE during build eventmgr/ domain/ Location.class location.hbm.xml copied here by IDE during build lib/ hibernate3.jar Hibernate requires several jars asm.jar ...
  • 11. Where to Get Hibernate http://www.hibernate.org Local copy: http://se.cpe.ku.ac.th/download/hibernate
  • 12. Required Hibernate Libraries  Libraries are in lib/ directory of Hibernate distribution.  Which ones do you need?  See _README.txt or my Using Hibernate notes.  In your IDE:  create a Hibernate "library"  add JARs to the library  better than copying JAR files to your project Required Libraries hibernate3.jar antlr-2.7.6.jar asm.jar cglib.jar commons-collections.jar commons-logging.jar dom4j.jar ehcache.jar jta.jar log4j.jar // and maybe xml-apis.jar
  • 13. Create a Hibernate Library in Eclipse 1. Project -> Properties -> Java Build Path 2. select Libraries tab. 3. Click "Add Library..." and select "User Library", Next> 4. Create a new user library named "Hibernate" 5. Add Jar files
  • 14. Add Library or jar for Database Driver  For Embedded Derby Database /path-to-derby/lib/derby.jar  For HSQLDB /path-to-hsqldb/lib/hsqldb.jar
  • 15. hibernate.cfg.xml for Embedded Derby DB <?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="dialect"> org.hibernate.dialect.DerbyDialect</property> <property name="connection.driver_class"> org.apache.derby.jdbc.EmbeddedDriver</property> <property name="connection.url"> jdbc:derby:/temp/eventmgr;create=true</property> <property name="hbm2ddl.auto">create</property> <!-- O-R mappings for persistent classes --> <mapping resource="eventmgr/domain/Location.hbm.xml"/> </session-factory> </hibernate-configuration> Remove after database created
  • 16. hibernate.cfg.xml for MySQL <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC ... remainder omitted > <hibernate-configuration> <session-factory> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="connection.username">student</property> <property name="connection.password">secret </property> <property name="connection.url"> jdbc:mysql://se.cpe.ku.ac.th/eventmgr </property> <!-- Object-Relational mappings for classes --> <mapping resource="eventmgr/domain/Location.hbm.xml"/> ... other mappings omitted </session-factory> </hibernate-configuration>
  • 17. XML Syntax, 1  XML Declaration: "this file contains XML" <?xml version='1.0' encoding='utf-8'?>  Root Element: "this whole file is a hibernate-configuration" <hibernate-configuration> content of document goes here <hibernate-configuration>  Child Elements: define content, have a tree-like nested structure <session-factory> ... </session-factory>  Tree structure & Scope: all elements must have a closing tag <class name="Event" table="EVENTS"> a start element <property name="location" /> element start & end </class> an end tag
  • 18. XML Syntax, 2  Attributes: values must always be quotes, no duplicates <class name="Event" table="EVENTS">  Special characters: < > & ' " must use reference except in CDATA <message> &quot;Hello &lt;b&gt;world&lt;/b&gt;&quot </message>  Child Elements can be repeated (depends on DTD) <courselist name="courses"> <course>object oriented programming</course> <course>software spec &amp; design</course> </courselist>  Elements can sometimes be written as attributes <course> <id>219341</id> <course id="219341"/> </course>
  • 19. Logging  Hibernate apps will log errors and/or activity.  Two choices:  Java SDK logging (since JDK 1.4)  Log4j  if you use Log4j, create a log4j.properties in your application source root directory.  Copy this file from the Hibernate etc/ directory.
  • 20. Sample log4j.properties  Too long to print here  See an actual file, for example: [hibernate.dir]/doc/tutorial/src/log4j.properties  Configuration logging of Hibernate: log4j.logger.org.hibernate=warn  Log the SQL generated by Hibernate: #log4j.logger.org.hibernate.SQL=debug  Log JDBC bind parameters (can be security leak): log4j.logger.org.hibernate.type=info
  • 21. Exercise 2  Define a Location class and LOCATIONS table  Write the class  Create a mapping file for Location: Location.hbm.xml
  • 22. Create the Location class Location class Location id: int name: String address: String LOCATIONS PK id INTEGER name VARCHAR(80) address VARCHAR(160) LOCATIONS table (Hibernate can auto-generate this)
  • 23. Write the Location class package eventmgr.domain; public class Location { private int id; private String name; private String address; public Location() { } // a no argument constructor public int getId( ) { return id; } public void setId( int id ) { this.id = id; } public String getName( ) { return name; } public void setName( String n ) { this.name = n; } public String getAddress( ) { return address; } public void setAddress( String a ) { address = a; } } Use JavaBean conventions in Persistent object classes.
  • 24. Hibernate can access private methods package eventmgr.domain; public class Location { private int id; private String name; private String address; public Location() { } public int getId( ) { return id; } private void setId( int id ) { this.id = id; } public String getName( ) { return name; } private void setName( String n ) { this.name = n; } public String getAddress( ) { return address; } private void setAddress( String a ) { address = a; } } OK to use "private" or "protected" for mutators.
  • 25. Hibernate can access private data, too public class Location { private int id; private String name; private void setName( String name ) { if ( name.length() < 3 ) new RuntimeException("name too short"); ... } Some mutator methods contain data validity checks or other complicated logic. to tell Hibernate to set the field values directly (don't use the "set" method) in the class mapping file write: <hibernate-mapping default-access="field"> ...
  • 26. Schema to create Locations table CREATE TABLE locations ( id INTEGER NOT NULL, name VARCHAR(80) NOT NULL, address VARCHAR(160), PRIMARY KEY(id) ) DEFAULT CHARSET=utf8 ; This works for MySQL. Hibernate can generate table schema at runtime. mysql> use eventmgr ; mysql> source tableschema.sql ;
  • 27. O-R Mapping for the Location class Map between object attributes and table columns. LOCATIONS PK id INTEGER name VARCHAR(80) address VARCHAR(160) Location id: int name: String address: String O-R Mapping requires a mapping file
  • 28. Mapping File Location.hbm.xml An XML file describing how to map object to table. Filename: Location.hbm.xml <!DOCTYPE hibernate-mapping PUBLIC ... remainder omitted > <hibernate-mapping package="eventmgr.domain"> <class name="Location" table="LOCATIONS"> <id name="id" column="id"> <!-- let hibernate choose id for new entities --> <generator class="native"/> </id> <property name="name" column="name" not-null="true"/> <property name="address"/> </class> </hibernate-mapping>
  • 29. Mapping File Explained <hibernate-mapping> <class name="eventmgr.domain.Location" table="LOCATIONS" options... > <id name="id" column="id" unsaved-value="0"> <generator class="native"/> </id> object attribute mappings </class> </hibernate-mapping> root element of a hibernate mapping mapping for one class Every persistent class needs an "identifier" attribute and column. The identifier is used to establish object identity (obj1 == obj2) and locate the table row for a persisted object. The id is usually the Primary Key of the table.
  • 30. Attribute Mapping: <property .../> <property name="name" column="name“ type=“name“ not-null="true“ /> You omit elements if Hibernate can guess the value itself: <property name="address" column="ADDRESS" type="string"/> <!-- omit data type and Hibernate will determine it --> <property name="address" column="ADDRESS"/> <!-- omit column if same as attribute (ignoring case)--> <property name="address"/> Name of the attribute in Java class Column name in the database table Hibernate or Java data type (usually you can omit it) Constraints
  • 31. What you have so far  Project configured for Hibernate  Hibernate configuration file  Location class and mapping file Location.hbm.xml  Configure a database schema (for learning, we will auto-generate schema)
  • 32. HibernateUtil: a Session Factory (1) HibernateUtil -sessionFactory: SessionFactory +getSessionFactory( ): SessionFactory +getCurrentSession( ): Session +openSession( ): Session // always use the same sessionFactory (Singleton) SessionFactory sessionFactory = new Configuration() .configure().buildSessionFactory(); // use sessionFactory to get a Session Session session = sessionFactory.openSession(); // or the more convenient: Session session = sessionFactory.getCurrentSession(); 1
  • 33. HibernateUtil: a Session Factory (2) public class HibernateUtil { private static SessionFactory sessionFactory = null; private static final Logger log = Logger.getLogger(..); public static SessionFactory getSessionFactory() { if ( sessionFactory == null ) { try { // Create the SessionFactory sessionFactory = new Configuration() .configure().buildSessionFactory(); } catch (Exception e) { System.err.println("sessionFactory error "+ ex); log.error("SessionFactory creation failed", ex); //throw new ExceptionInInitializerError(ex); } } return sessionFactory;
  • 34. HibernateUtil: a Session Factory (3) /** * Get the current Hibernate session. * This creates a new session if no current session. * @return the current Hibernate Session */ public static Session getCurrentSession() { return getSessionFactory().getCurrentSession(); } public static Session openSession() { return getSessionFactory().openSession(); }
  • 35. Persistence Test: LocationTest.java public class TestLocation { public static void testSave() { Location loc1 = new Location( ); loc1.setName("Kasetsart University"); loc1.setAddress("90 Pahonyotin Rd, Bangkok"); Location loc2 = new Location(); loc2.setName("UCLA"); loc2.setAddress("Westwood, California"); Session session = HibernateUtil.getCurrentSession(); Transaction tx = session.beginTransaction(); session.saveOrUpdate( loc1 ); session.saveOrUpdate( loc2 ); tx.commit(); System.out.println("Locations saved"); }
  • 36. Persistence Test: LocationTest.java public static void testQuery() { System.out.println("Retrieving locations"); Session session = HibernateUtil.getCurrentSession(); Transaction tx = session.beginTransaction(); Query query = session.createQuery("from Location"); // query.list() returns objects, cast to List<Location> List<Location> list = (List<Location>)query.list( ); tx.commit(); for(Location l : list ) out.printf("%d %s %sn", l.getId(), l.getName(), l.getAddress() ); if ( session.isOpen() ) session.close(); } public static void main( String [] args) { testSave( ); testQuery( );
  • 37. Exercise 3  Test object uniqueness:  In one session get the same location two times and compare using (ku1 == ku2). Are they same?  In different sessions get the same location and compare (ku1 == ku2). Are they same?  Test transparent persistence:  Modify a location inside a session. Does database change?  Reattach a modified object to new session. Does database change?
  • 38. Getting a Unique Result Session session = HibernateUtil.getCurrentSession(); Query query = session.createQuery( "from Location where name=‘Kasetsart University’"); // query.uniqueResult() returns only first match Location ku1 = (Location) query.uniqueResult( ); Session session = HibernateUtil.getCurrentSession(); Query query = session.createQuery( "from Location l where l.name like 'Kasetsart%'"); // query.uniqueResult() returns only first match Location ku1 = (Location) query.uniqueResult( ); Pattern matches "like" - use "%" as wildcard character
  • 39. Mapping a Class with Associations Simplified version of Event class. Event id: long name: String startDate: Date location: Location Location id: int name: String address: String 1 *
  • 40. O-R Mapping of n-to-1 Associations Event id: long name: String startDate: Date location: Location LOCATIONS PK id INTEGER name VARCHAR address VARCHAR Location id: int name: String address: String EVENTS PK id BIGINT name VARCHAR start_date TIMESTAMP FK location_id INTEGER The Data Mapper converts a n-to-1 association to a foreign key relation (persist) or foreign key to object (retrieve). 1 *
  • 41. Mapping for Event (Event.hbm.xml) Use <many-to-one name="attribute" ... /> to map a reference to another object. <!DOCTYPE hibernate-mapping PUBLIC ... remainder omitted > <hibernate-mapping package="eventmgr.domain"> <class name="Event" table="EVENTS"> ... <property name="startDate" column="start_date" type="timestamp" /> <many-to-one name="location" column="location_id" class="Location" /> </class> </hibernate-mapping> you can omit class (Hibernate can determine itself)
  • 42. Test: Save an Event public static void testSave() { Location loc1 = new Location( ); loc1.setName("Kasetsart University"); loc1.setAddress("90 Pahonyotin Rd, Bangkok"); Event event = new Event( ); event.setName("Java Days"); event.setLocation( loc1 ); event.setStartDate( new Date(108,Calendar.JULY, 1) ); Session session = HibernateUtil.getCurrentSession(); Transaction tx = session.beginTransaction(); session.saveOrUpdate( event ); tx.commit(); System.out.println("Event saved"); }
  • 43. Did you get an Error? The Location doesn't exist in database (transient object). Exception in thread "main“ org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: eventmgr.domain.Location
  • 44. Persisting the Event location Solutions: 1. save location during the transaction (manual save) 2. tell Hibernate to cascade the save operation (automatically save Location) <many-to-one name="location" column="location_id" class="Location" cascade="save-update" /> cascade="none" don't cascade operations "all" cascade all operations (be careful) "save-update" cascade save and updates "delete-orphan" cascade all, delete unreferenced orphan children
  • 45. Test: Retrieve an Event public static void testRetrieve() { System.out.println("Retrieving event"); Session session = HibernateUtil.getCurrentSession(); Transaction tx = session.beginTransaction(); Query query = session.createQuery( "from Event e where e.name= :name"); query.setParameter("name", "Java Days"); // query.list() returns objects, cast to List<Location> List<Event> list = (List<Event>)query.list( ); tx.commit(); for(Event e : list ) out.printf("%d %s %sn", e.getId(), e.getName(), e.getLocation().getName() ); }
  • 46. Lazy Instances and Proxies Transaction tx = session.beginTransaction(); Query query = session.createQuery( "from Event e where e.name=:name"); query.setParameter("name", "Java Days"); List<Event> list = (List<Event>)query.list( ); tx.commit(); for(Event e : list ) out.printf("%d %s %sn", e.getId(), e.getName(), e.getLocation().getName() ); Error: LazyInstantiationException • Hibernate uses lazy instantiation and proxy objects • Hibernate instantiates the location object when it is first accessed • We closed the transaction before accessing the location
  • 47. Two Solutions 1. Modify our code: getLocation( ) before closing the session. 2. Tell Hibernate not to use lazy instantiation of Location objects (in Location.hbm.xml) List<Event> list = (List<Event>)query.list( ); for(Event e : list ) out.printf("%d %s %sn", e.getId(), e.getName(), e.getLocation().getName() ); tx.commit( ); <class name="Location" table="LOCATIONS" lazy="false"> ... </class>
  • 48. Creating a Component for Address Address street: String city: String province: String Location id: int name: String address: Address 1 * LOCATIONS id name address 101 Kasetsart University street city province 102 Seacon Square
  • 49. Organizing your Work Use DAO to separate OR behavior from the rest of your project.
  • 50. A DAO for the Location class The "useful" methods depend on the domain class and the application. LocationDao findById(id: int) : Location findByName(name : String): Location[*] find(query: String) : Location[*] save(loc: Location) : boolean delete(loc: Location) : boolean
  • 51. Java code for Simple LocationDao package eventmgr.dao; import ...; public class SimpleLocationDao { private static final Logger logger = Logger.getLogger(LocationDao.class); public LocationDao() { } public Location findById( int id ) public List<Location> findByName( String name ) public List<Location> find( String query ) public boolean save( Location loc ) public boolean delete( Location loc ) }
  • 52. The core of findById( ) - use "load" public Location findById( int id ) { Location result = null; Session session = null; Transaction tx = null; try { session = HibernateUtil.getCurrentSession(); tx = session.beginTransaction(); result = (Location)session.load(Location.class, id); tx.commit(); session.close( ); } catch ... } return result; }
  • 53. The details of findById( ) public Location findById( int id ) { Location result = null; Session session = null; Transaction tx = null; try { session = HibernateUtil.getCurrentSession(); tx = session.beginTransaction(); result = (Location)session.load(Location.class,id); tx.commit(); } catch (ObjectNotFoundException e) { logger.info("Object not found. id = "+id, e); if ( tx != null && ! tx.wasCommitted() ) tx.rollback(); } catch (HibernateException e) { logger.error("Hibernate exception", e); if ( tx != null && ! tx.wasCommitted() ) tx.rollback(); } finally { if ( session != null && session.isOpen() ) session.close(); } return result; }
  • 54. The core of findByName( ) - "query" public List<Location> findByName( String name ) { List<Location> result; ... try { session = HibernateUtil.getCurrentSession(); tx = session.beginTransaction(); Query query = session.createQuery( "from Location where name=:name" ); query.setParameter( "name", name ); result = (List<Location>) query.list( ); tx.commit(); session.close( ); } catch ... return result; }
  • 55. Details of findByName( )  Exercise
  • 56. The core of save( ) - "saveOrUpdate" public boolean save( Location location ) { boolean result = false; ... try { session = HibernateUtil.getCurrentSession(); tx = session.beginTransaction(); session.saveOrUpdate( location ); tx.commit(); session.close( ); result = true; } catch ... return result; }
  • 58. The core of delete( ) - "delete" public boolean delete( Location location ) { boolean result = false; ... try { session = HibernateUtil.getCurrentSession(); tx = session.beginTransaction(); session.delete( location ); tx.commit(); session.close( ); result = true; } catch ... return result; }
  • 59. Redundant Code  Every DAO method has the same boilerplate code: Session session = null; Transaction tx = null; try { session = HibernateUtil.getCurrentSession(); tx = session.beginTransaction(); do the work here tx.commit(); } catch (ObjectNotFoundException e) { logger.info("Object not found. "+id, e); if ( tx != null && ! tx.wasCommitted() ) tx.rollback(); } catch (HibernateException e) { logger.error("Hibernate exception", e); if ( tx != null && ! tx.wasCommitted() ) tx.rollback(); } finally { if ( session != null && session.isOpen() ) session.close(); }
  • 60. Factor out Redundant Code Class SimpleLocationDao { private Session session; // declare as attributes private Transaction tx; public Location findById( int id ) { try { beginTransaction( ); do the work here commitTransaction( ); } catch (ObjectNotFoundException e) { handleError( e ); } catch (HibernateException e) { handleError( e ); } finally { if ( session != null && session.isOpen() ) session.close(); }
  • 61. Duplicate Code Between DAO  In every DAO, the CRUD methods are almost the same  Consider save( ) ... public boolean save( Location location ) { boolean result = false; ... try { beginTransaction( ); session.saveOrUpdate( location ); commitTransaction( ); } catch ( ... ) { handleException( e ); } finally { ... } return result; }
  • 62. Apply the Layer Superclass Pattern AbstractDao #load( class, id ) : Object #find( class, query ) : List #saveOrUpdate( object: Object ) #delete( object: Object ) LocationDao EventDao SpeakerDao
  • 63. AbstractDao.save protected Object save( Object obj ) { Object result = null; try { beginTransaction(); result = session.saveOrUpdate( obj ); commitTransaction(); } catch (ObjectNotFoundException e) { handleError( e ); } catch (HibernateException e) { handleError( e ); } finally { closeSession( ); } return result; }
  • 64. LocationDao using Layer Superclass public class LocationDao extends AbstractDao { public boolean save( Location location ) { return super.save( location ); }
  • 65. AbstractDao.load protected Object load( Class clazz, Serializable id ) { Object result = null; try { beginTransaction(); result = session.load( clazz, id); commitTransaction() } catch (ObjectNotFoundException e) { handleError( e ); } catch (HibernateException e) { handleError( e ); } finally { closeSession( ); } return result; }
  • 66. LocationDao using Layer Superclass public class LocationDao extends AbstractDao { public LocationDao( ) { super( ); } public Location findById( int id ) { return (Location) super.load( Location.class, id ); }
  • 67. Exercise  use your SimpleLocationDao to write a layer superclass named AbstractDao.  write a LocationDao that extends AbstractDao
  • 68. Hibernate Query Language (HQL) Query query = session.createQuery( "from Event where name='Java Days'"); Query query = session.createQuery( "select e from Event e where e.name='Java Days'");  Hibernate queries you Hibernate Query Language (HQL).  HQL is object-centric - use class and property names, not SQL table names.
  • 69. HQL example String query = "select e from Event e where e.location.name = 'Kasetsart University'"; Query q = session.createQuery( query ); String sqlquery = "select * from EVENTS e join LOCATIONS l ON e.location_id = l.id where l.name = 'Kasetsart University'"; Statement stmt = connection.createStatement( sqlquery ); Problem: Find all events which are held at Kasetsart HQL: SQL and JDBC:
  • 70. Many-to-one Associations public class Event { private long id; private String name; private Location location; // 1-to-many assoc. private Date startDate; private long duration; public Event( ) { } ... }
  • 71. Event.hbm.xml Mapping File <!DOCTYPE hibernate-mapping PUBLIC ... remainder omitted > <hibernate-mapping package="eventmgr.domain"> <class name="Event" table="EVENTS"> <id name="id" column="id"> <generator class="native"/> </id> <property name="name" column="name" not-null="true"/> <property name="startDate" column="start_date" column="timestamp"/> <property name="duraction" /> <many-to-one name="location" column="location_id" class="Location"/> </class> </hibernate-mapping>
  • 72. Cascading Save & Update <class name="Event" table="EVENTS"> <id name="id" column="id"> <generator class="native"/> </id> ... <many-to-one name="location" column="location_id" class="Location" cascade="save-update"/> </class> When you save an Event, should Hibernate automatically save the location, too? • no: then you must save the location yourself • Yes: specify cascade = "save-update" Other choices for cascade: all, none, save-update, delete
  • 73. Learning Hibernate  Tutorial at www.hibernate.org  Another good tutorial: http://www.allapplabs.com/hibernate/hibernate_tutorials .htm  Peak & Heudecker, Hibernate Quickly, 2006.  Baur & King, Java Persistence with Hibernate, 2007, update of Hibernate in Action, and much longer.  Baur & King, Hibernate in Action, 2005, considered the best Hibernate book, by one of the Hibernate creators.
  • 74. Hibernate Tools Hibernate Tools Eclipse plugin - HibernateConsole and more  Test HQL queries, browse database Middlegen - generates class mapping files (hbm.xml) from an existing database schema. Has Eclipse plugin. Hibernate Synchronizer - generates class mapping files (hbm.xml) and Java classes from a database schema. hbm2ddl - creates a database schema (in SQL) from a hbm.xml file. hbm2java - creates a Java class from a hbm.xml file.