Advanced Hibernate
Advanced Hibernate
Session 02 / 5.6.2006
Advanced Hibernate
By : Yanai Franchi, Senior Software Engineer “Tikal”
Agenda
Overview and Recap
Advanced Mappings
Transactional Processing
Querying and Fetching
Tuning
!=
The Object-Relational impedance mismatch
» How do we map tables and column to objects
Hibernate in a Nutshell
A solid product, widely used
Use a mapping layer to map between objects and
tables
» XML
» Annotations
Dual-Layer Caching Architecture (HDLCA)
Powerful, high performance queries
Support for detached objects (no DTOs)
…and many more cool features
Provide a great programming model
» Non invasive
» Transparent and transitive
Hibernate API
hibernate.cfg
hbm files
Hibernate Mapping
package com.tikal.sample;
public class User {
private Long id;
private String name;
public User(){}
public User(name){
this.name=name;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
<hibernate-configuration> Hibernate.cfg
<session-factory>
<property name="dialect">
org.hibernate.dialect.HSQLDialect</property>
<property name="connection.driver_class">
org.hsqldb.jdbcDriver
</property>
<property name="connection.url">
jdbc:hsqldb:mem:aname
</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<mapping resource=“model/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Hosted by Tikal | 8 | www.tikalk.com
Israel JBUG
Session s = sf.openSession();
Transaction t = s.beginTransaction();
u = new User("Yossi");
s.save(u);
t.commit();
s.close();
sf.close();
persistence.xml hibernate.cfg
EJB3 Annotations hbm files
What is JPA ?
Java Persistence API is now the standard API
for ORM for the Java EE platform.
» Simplifies the development of JEE and JSE
applications using data persistence.
» Get the entire Java community behind a single,
standard persistence API
Originated as an improvement of EJB-CMP
and became a separate persistence spec for
POJOs.
Usable both within JSE5 and JEE5
We can use Hibernate (or other ORM
implementation) as the persistence
provider.
Hosted by Tikal | 11 | www.tikalk.com
Israel JBUG
JPA Mapping
import javax.persistence.*;
package com.tikal.sample;
@Entity @Table(name=“USERS")
public class User{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
User(){}
public User(name){
this.name=name;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
Hosted by Tikal | 12 | www.tikalk.com
Israel JBUG
<persistence-unit name="manager1“
transaction-type=“RESOURCE_LOCAL”>
<class>com.tikal.sample.User</class>
<properties>
<property name="hibernate.dialect"
value="org.hibernate.dialect.HSQLDialect" />
<property name="hibernate.connection.driver_class"
value="org.hsqldb.jdbcDriver" />
<property name="hibernate.connection.url"
value="jdbc:hsqldb:mem:aname" />
<property name="hibernate.connection.username" value="sa" />
<property name="hibernate.connection.password" value="" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.cache.provider_class"
value="org.hibernate.cache.NoCacheProvider" />
</properties>
</persistence-unit>
</persistence>
JPA in Action
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("manager1");
EntityManager em = emf.createEntityManager();
em.persist(u);
User u2 = (User)em.find(User.class,u.getId());
em.close();
emf.close();
persistence.xml hibernate.cfg
EJB3 Annotations hbm files
hibernate.cfg
EJB3+Hibernate+Validation
Hibernate Annotations
import javax.persistence.*;
@Entity @Table(name=“USERS")
@org.hibernate.annotations.Entity(mutable=false)
public class User{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
User(){}
public User(name){
this.name=name;
}
public void setName(String name) {
this.name = name;
}
@org.hibernate.validator.Length(max=8)
@org.hibernate.validator.NotNull
public String getName() {
return name;
}
}
Hosted by Tikal | 16 | www.tikalk.com
Israel JBUG
SessionFactory sf =
new AnnotationConfiguration().buildSessionFactory();
Session s = sf.openSession();
Transaction t = s.beginTransaction();
u = new User("Yossi");
s.save(u); //hibernate api
s.persist(u); // Java Persistent api
t.commit();
s.close();
sf.close();
Hosted by Tikal | 17 | www.tikalk.com
Israel JBUG
Dynamic Models
Dynamic Models
» Map of Maps
» Dom4j trees
» Implement your own Tuplizer
pojoSession.getSession(EntityMode.MAP);
Dynamic-Model Example
n 1
Customer Organization
<hibernate-mapping>
…
<class entity-name="Customer">
<id name="id" type="long" column="ID">
<generator class="sequence"/>
</id>
<property name=“firstName" column=“FIRST_NAME"/>
<property name=“lastName" column=“LAST_NAME"/>
<many-to-one name=“relatedOrganization”
cascade=“save-update“ class="Organization"/>
</class>
</hibernate-mapping>
Hosted by Tikal | 19 | www.tikalk.com
Israel JBUG
s.save("Customer", myCustomer);
tx.commit();
s.close();
Transactional
Processing
Persistence by Reachability
An Object becomes persistence if it is referenced
from a persistent instance 1
n
Category
addCategories
Identity Problem
Java objects define two different notions of
sameness:
» Object identity (a == b)
» equality by value ( equals() method )
The identity of a database row is expressed as
the primary key value.
Primary Key
public class User {
...
public boolean equals(Object other) {
if (this==other) return true;
if (id==null) return false;
if ( !(other instanceof User) ) return false;
final User that = (User) other;
return this.id.equals( that.getId() );
}
public int hashCode() {
return (id==null) ?
System.identityHashCode(this) :
id.hashCode();
}
}
Business Key
public class User {
...
public boolean equals(Object other) {
if (this==other) return true;
if ( !(other instanceof User) ) return false;
final User that = (User) other;
return this.name.equals( that.getName() );
}
public int hashCode() {
return name.hashCode();
}
}
1 n
Category Item
List<Category> categories =
session.createQuery("from Category“).list();
for (Category category : categories) {
for (Item item :category.getItems()) {
if(item.getPrice >123) //doSomething
}
}
Eager Fetching
Caching Scope
1
n
Category
displayCategoryTree(root);
closeSession();
List<Customer> customers =
session.getNamedQuery( "GetCustomers").list()
tx.commit();
session.close();
StatelessSession
Lower-level abstraction, much closer to the
underlying JDBC.
No associated persistence context.
Does not implement a first-level cache nor interact
with any second-level or query cache.
Does not implement transactional write-behind or
automatic dirty checking.
Operations do not ever cascade to associated
instances.
Collections are ignored by a stateless session.
Bypass Hibernate's event model and interceptors.
insert(), update() and delete() have different
semantics than save(), saveOrUpdate() and delete()
StatelessSession Example
StatelessSession session =
sessionFactory.openStatelessSession();
Transaction tx = session.beginTransaction();
ScrollableResults customers = session
.getNamedQuery("GetCustomers")
.scroll(ScrollMode.FORWARD_ONLY);
while (customers.next()) {
Customer customer = (Customer) customers.get(0);
customer.updateStuff(...);
session.update(customer);
}
tx.commit();
session.close();
DML-Style Operations
Filtering Data
<filter-def name="effectiveDate">
<filter-param name="asOfDate" type="date"/>
</filter-def>
Filtering in Action
Q&A
Thank You
[email protected]