Inf5750 - Lecture 2. C - Hibernate Intro PDF
Inf5750 - Lecture 2. C - Hibernate Intro PDF
Mapping (ORM)
and Hibernate
INF5750/9750 - Lecture 2 (Part III)
Problem area
● When working with object-oriented systems, there’s a
mismatch between the object model and the relational
database
● How do we map one to the other?
STUDENT DEGREE
student_id degree_id
name type
Relational
address name database
degree_id
Need for ORM
● Write SQL conversion methods by hand using JDBC
○ Tedious and requires lots of code
○ Extremely error-prone
○ Non-standard SQL ties the application to specific databases
○ Vulnerable to changes in the object model
○ Difficult to represent associations between objects
Magic happens
Degree
here!
ORM / Hibernate
(Database)
Example app: The EventManager
Java Hibernate
objects API
Hibernate Hibernate
mapping files configuration
Java objects (POJO)
public class Event
Identifier property {
(optional) private int id;
private String title;
private Date date;
private Set<Person> persons = new HashSet<Person>();
No-argument constructor
(required) public Event() {
}
Java Hibernate
objects API
Hibernate Hibernate
mapping files configuration
Hibernate mapping files
● Tells Hibernate which tables and columns to use to load
and store objects
<!DOCTYPE hibernate-mapping PUBLIC
DTD "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
Class element <class name=”no.uio.inf5750.Event” table=”events”>
</class>
</hibernate-mapping>
Filename: Event.hbm.xml
Property mapping
The name property refers Title must be
to the get/set-methods not null and unique
...
<property name=”title” not-null=”true” unique=”true”/>
<property name=”date” type=”Date” column=”event_date”/>
...
<many-to-many column=”person_id”
Column name class=”no.uio.inf5750.example.model.Person”/>
for ”other” side
</set>
of association
...
Reference to the
associated class
Hibernate mapping types
● Hibernate will translate Java types to SQL / database types
for the properties of your mapped classes
Java Hibernate
objects API
Hibernate Hibernate
mapping files configuration
Hibernate configuration
● Also referred to hibernate properties
● Each database has a dialect
○ hibernate.dialect = org.hibernate.dialect.H2Dialect
● Must also specify:
○ JDBC driver class
○ Connection URL
○ Username
○ Password
● More later in the lecture...
Example app: The EventManager
Java Hibernate
objects API
Hibernate Hibernate
mapping files configuration
The SessionFactory interface
● When all mappings have been parsed by the org.
hibernate.cfg.Configuration, the application must obtain
a factory to get org.hibernate.Session
● SessionFactory provides org.hibernate.Session
instances to the application
● Shared among application threads
● Most important method is getCurrentSession
SessionFactory sessionFactory = cfg.buildSessionFactory();
Make a transient object Event event = new Event( ”title”, new Date() );
persistent
Integer id = (Integer) session.save( event );
Load an object – if
Event event = (Event) session.load( Event.class, id );
matching row exists
Load an object – if
Event event = (Event) session.get( Event.class, id );
unsure about matching row
Transaction begins
(SUCCESS)
Transaction commit
Hibernate in real-life apps
● Spring used for SessionFactory management
○ Spring has excellent ORM integration
○ Custom SessionFactory management is “boilerplate-code”
● Spring used for Transaction management
○ Custom tx management is error prone
○ Support for declarative tx management with annotations
○ Consistent programming model across JTA, JDBC, Hibernate
● Annotate transactional methods / class with @Transactional
● Hibernate’s own connection pooling is basic. Hibernate allows
external JDBC pooling through library called C3P0
Spring-Hibernate dependencies
...
<dependency>
... <groupId>org.hibernate</groupId>
<dependency> <artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
<groupId>org.springframework</groupId> </dependency>
<artifactId>spring-core</artifactId> <dependency>
<version>${spring.version}</version> <groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency> <version>3.5.1-Final</version>
<dependency> </dependency>
<groupId>org.springframework</groupId> <dependency>
<groupId>geronimo-spec</groupId>
<artifactId>spring-context</artifactId> <artifactId>geronimo-spec-jta</artifactId>
<version>${spring.version}</version> <version>1.0-M1</version>
</dependency> </dependency>
<dependency>
<dependency> <groupId>c3p0</groupId>
<groupId>org.springframework</groupId> <artifactId>c3p0</artifactId>
<artifactId>spring-tx</artifactId> <version>0.9.1.2</version>
</dependency>
<version>${spring.version}</version> <dependency>
</dependency> <groupId>org.slf4j</groupId>
<dependency> <artifactId>slf4j-log4j12</artifactId>
<version>1.5.8</version>
<groupId>org.springframework</groupId> </dependency>
<artifactId>spring-orm</artifactId> <dependency>
<version>${spring.version}</version> <groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency> <version>1.2.136</version>
</dependency>
Spring-Hibernate configuration
<tx:annotation-driven transaction-manager="transactionManager"/>
Enables
annotations
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
<property name="dataSource" ref="dataSource"/>
</bean>
Can be
injected
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> like any bean
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>hibernate/Event.hbm.xml</value>
<value>hibernate/Person.hbm.xml</value> Points to
</list>
</property> mapping files
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">create-drop</prop> Hibernate properties
</props>
</property> ● H2 database (fast inmemory DB)
</bean> ● Automatically create tables
Java Hibernate
objects API
Hibernate Hibernate
mapping files configuration
Advantages of ORM
● Productivity
○ Eliminates lots of repetitive code – focus on business logic
○ Database schema is generated automatically
● Maintainability
○ Fewer lines of code – easier to understand
○ Easier to manage change in the object model
● Database vendor independence
○ The underlying database is abstracted away
○ Can be configured outside the application
● Performance
○ Lazy loading – associations are fetched when needed
○ Caching
Resources
● Spring documentation chapter 13
http://static.springsource.org/spring/docs/3.2.x/spring-
framework-reference/html/orm.html