hands-on hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · open source in the...

84
Open Source in the Corporate World Hands-On Hibernate Brian McCallister

Upload: others

Post on 26-May-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

Open Source in the Corporate World

Hands-On Hibernate

Brian McCallister

Page 2: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Agenda

• Super Quick Intro

• Thinking in Object Graphs

• Transactions (so Overloaded!)

• Inheritance and Polymorphism

• Hibernate Interceptor

• Filters

• Performance Tuning

2

Mostly Hibernate 3.0

Page 3: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

Open Source in the Corporate World

Super Quick Intro

Don’t Blink!

Page 4: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Transparent Persistence

• Persists regular objects

• Works with object graphs

• Tracks object state for you

• Isn’t really transparent!

4

Page 5: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

The Boilerplate

5

// Only ever need to do onceSessionFactory factory = new Configuration() .configure() .buildSessionFactory();

// A Session is like a JDBC ConnectionSession session = factory.openSession();

Transaction tx = session.beginTransaction();try { ... do stuff ... tx.commit();} catch (Exception e) { tx.rollback(); throw e;} finally { session.close();}

Page 6: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Creating Stuff

6

public void testCreate() throws Exception { assertEquals(0, Tools.countRows("beer")); Beer coors = new DomesticBeer("Coors", 0.75);

Transaction tx = session.beginTransaction(); session.save(coors); tx.commit();

assertEquals(1, Tools.countRows("beer"));}

Page 7: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Reading Stuff, Part 1

7

session.createQuery("select b from Beer b " + "where b.price < :price") .setBigDecimal("price", new BigDecimal(“1.0”)) .list();...

select beer0_.id as id, beer0_.brand as brand2_, beer0_.price as price2_, beer0_.country as country2_, beer0_.duty as duty2_, beer0_.type as type from beer beer0_ where (beer0_.price<?)

Page 8: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Reading Stuff, Part 2

8

session.createQuery("select b from Beer b " + " inner join fetch b.truck " + "where " + " b.truck.driverName = :name") .setString("name", "George") .list();

...

select [FIELDS OMITTED]from beer beer0_ inner join trucks truck1_ on beer0_.truck_id=truck1_.id, trucks truck2_ where ( truck2_.driver_name=? and beer0_.truck_id=truck2_.id)

Page 9: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Reading Stuff, Part 3

9

Beer beer = (Beer) session.load(Beer.class, new Integer(7));

...

Beer same = (Beer) session.get(Beer.class, new Integer(7));

Page 10: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Updating Stuff

10

// PropertiesTransaction tx = session.beginTransaction();Truck t = (Truck) session.load(Truck.class, new Integer(17));t.setDriverName("Brian McCallister");tx.commit();

// Relationshipstx = session.beginTransaction();Beer beer = (Beer) session.load(Beer.class, new Integer(15));Truck truck = (Truck) session.load(Truck.class, new Integer(17));truck.getCargo().add(beer);beer.setTruck(truck);tx.commit();

Page 11: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Deleting Stuff

11

Transaction tx = session.beginTransaction();Truck truck = (Truck) session.load(Truck.class, new Integer(17));session.delete(truck);tx.commit();

Page 12: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Always with the XML...

12

<hibernate-mapping> <class name="com.example.Truck" table="trucks" discriminator-value="not null"> <id name="id" type="integer" unsaved-value="null"> <generator class="native"/> </id> <discriminator column="driver_name" insert="false"/> <property name="driverName" column="driver_name"/> <bag name="cargo" lazy="true" cascade="persist" inverse="true"> <key column="truck_id"/> <one-to-many class="com.example.Beer"/> </bag> <subclass discriminator-value="null" name="com.example.AutoPilotTruck"/> </class></hibernate-mapping>

Page 13: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Transparent Persistence

• Persists regular objects

• Works with object graphs

• Tracks object state for you

• Isn’t really transparent!

– But is awfully close!

13

Page 14: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

Open Source in the Corporate World

Thinking in Object Graphs

Page 15: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Domain Model

15

Beer

idbrandprice

DomesticBeerImportedBeer

countryduty

Truck

driverNamecargoload(..)unload()

Pub

stockdebtreceive(..)

**

Page 16: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Regular Objects

16

public class DomesticBeer implements Beer { private Integer id; private String brand; private BigDecimal price; private Truck truck;

public DomesticBeer() { this("Schlitz", 0.50); }

public DomesticBeer(String brand, BigDecimal price) { this.brand = brand; this.price = price; }

public String getBrand() { return brand; }

public void setBrand(String brand) { this.brand = brand; }

...

Page 17: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Object Graphs

17

TruckFleet

Beer

Driver

Beer

Beer

TruckFleet

Beer

Beer

Beer

Beer

Truck

Beer

Beer

Beer

Beer

System

Graph 2

Graph 1

Page 18: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Persistent States

18

• Transient

• Persistent Clean

• Persistent Dirty

• Persistent New

• Persistent Deleted

• Detached

Page 19: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

For Example...

19

Session

Dirty

Truck

DirtyBeer

Clean

New

Beer

Beer

Detached

Beer

TransientBeer

Detached

Truck

Page 20: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Cascades

20

• Persistence by Reachability

• Define graph traversal behavior

• Configurable per-relationship

• Apply to Session method calls

• Does not apply to state tracking

Page 21: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Cascades and Graphs

21

TruckFleet

Beer

Driver

Beer

Beer

create

create

create

all,delete-orphan

save-update

none

none

Page 22: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Traversing the Graph

22

• Cascades– create– merge– delete– save-update– evict– replicate– lock– refresh– delete-orphan– all

Page 23: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Relationships

• Object Reference

• Collections

• One-Way

• Two-Way

23

Page 24: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Relationships

24

Page 25: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Who Controls Things?

25

<class name="com.example.Truck" .. /> .. <bag name="cargo" inverse="true"> <key column="truck_id" /> <one-to-many class="com.example.Beer" /> </bag> ..</class>

...

<class name="com.example.Beer" .. /> .. <many-to-one name="truck" column="truck_id"/> ..</class>

Page 26: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Parent-Child

• Dependent Entities

• Usually < ..cascade=”all, delete-orphan” ..>

26

Truck

history: Collection<Delivery>

Delivery

truck: Truck

*

1

Page 27: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Object Graphs

• Need to think in terms of graphs

– At least a little

• Be aware of nature of relationships

• Be aware of object state

27

Page 28: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

Open Source in the Corporate World

Transactions(so overloaded)

Page 29: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

These are all “Transactions”

• Unit of Work

• Database Transaction

• Local Transaction

• XA Transaction

– UserTransaction

• Managed Transactions

– CMT & Spring

29

Page 30: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Sessions and Changes

• Track changes

• Flushes changes explicitly

• Flushes changes on transaction commits

• Sometimes flushes changes on queries

• Does not flush changes on close

– by default, can be made to

30

Page 31: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Sessions and Transactions

• Sessions can span multiple transactions

– Explicit or Managed

• Sessions can span multiple connections!

– Long Session

31

Page 32: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Long Sessions

32

session.setFlushMode(FlushMode.NEVER);tx = session.beginTransaction();Truck t = new Truck();t.getCargo().add(new DomesticBeer());session.save(t);tx.commit();session.disconnect();

assertEquals(0, Tools.countRows("trucks"));assertEquals(0, Tools.countRows("beer"));

session.reconnect();tx = session.beginTransaction();t.setDriverName("Chris");session.flush();tx.commit();

assertEquals(1, Tools.countRows("trucks"));assertEquals(1, Tools.countRows("beer"));

Page 33: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Long Sessions

33

• Long-Running Unit of Work

• Accumulates changes across transactions

• Manually Demarcated

• “Logical Transaction”

Page 34: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

However...

• Same Session used elsewhere

– Session tracking

– Separate Concerns

• Concurrent Modifications

• Manual Demarcation

• Mixed Strategies

• “Rollback” == Don’t Flush

34

Page 35: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

For Inserts

35

tx = session.beginTransaction();Truck t = new Truck();t.getCargo().add(new DomesticBeer());tx.commit();session.close();

// New Request

session = factory.openSession();tx = session.beginTransaction();session.save(t);tx.commit();

assertEquals(1, Tools.countRows("trucks"));assertEquals(1, Tools.countRows("beer"));

Reasonable Cascades

Page 36: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Using Long Sessions

36

• Special Session

• Attached to “Wizard” Context

• Prefer for non-contentious updates

• Do not use by default!

Page 37: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Managing Transactions

• Local

– session.beginTransaction()

• Bean Managed Transactions

– UserTransaction

• Container Managed Transactions

– Declarative on EJBs

• Proprietary Managed

– Spring, AspectJ, etc

37

Page 38: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Rules of Thumb

• Same as database transactions

– It is, really

• Prefer declarative transactions

– CMT and Spring in particular

• Avoid Long Session unless you need it

– Then use it!

38

Page 39: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

Open Source in the Corporate World

Inheritance and

Polymorphism

Page 40: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Extents

• Table-Per-Hierarchy

• Table-Per-Class

• Table-Per-Subclass

• Implicit

40

Page 41: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Type Hierarchy

41

Beer

idbrandpricetruck

DomesticBeerImportedBeer

countryduty

Interface

Classes

Page 42: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Table Per Hierarchy

42

BEER

id

brandpricetruck_idcountrydutytype

Page 43: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Table Per Hierarchy

43

<class name="com.example.Beer" table="beer" abstract="true"> <id name="id" type="integer"> <generator class="native"/> </id> <discriminator column="type"/> <property name="brand"/> <property name="price"/> <many-to-one name="truck" column="truck_id"/>

<subclass name="com.example.DomesticBeer" discriminator-value="DOMESTIC"/> <subclass name="com.example.ImportedBeer" discriminator-value="IMPORTED"> <property name="country"/> <property name="duty"/> </subclass></class>

Page 44: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Table Per Subclass

44

Page 45: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Table Per Subclass

45

<class name="com.example.Beer" table="beer" abstract="true" > <id name="id" type="integer" unsaved-value="null"> <generator class="native"/> </id> <property name="price"/> <many-to-one name="truck" column="truck_id"/>

<joined-subclass name="com.example.DomesticBeer" table="domestic_beer"> <key column="beer_id"/> </joined-subclass>

<joined-subclass name="com.example.ImportedBeer" table="imported_beer"> <key column="beer_id" /> <property name="country"/> <property name="duty"/> </joined-subclass></class>

Page 46: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Table Per Concrete Class

46

IMPORTED_BEER

id

brandpricetruck_idcountryduty

DOMESTIC_BEER

id

brandpricetruck_id

Page 47: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Table Per Concrete Class

47

<class name="com.example.Beer" abstract="true"> <id name="id" type="integer" unsaved-value="null"> <generator class="native"/> </id> <version name="version" column="version"/>

<property name="brand"/> <property name="price"/> <many-to-one name="truck" column="truck_id"/>

<union-subclass name="com.example.DomesticBeer" table="domestic_beer" />

<union-subclass name="com.example.ImportedBeer" table="imported_beer"> <property name="country"/> <property name="duty"/> </union-subclass></class>

Page 48: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Polymorphic Relationships

• Truck is the same across all of them!

• Map relationship to top of extent

– The Beer

48

Page 49: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Relations With Beer

49

<class name="com.example.Truck" .. > ... <bag name="cargo" cascade="persist"> <key column="truck_id" /> <one-to-many class="com.example.Beer"/> </bag> ...</class>

...

public class Truck implements Identifiable { ... private Collection cargo = new ArrayList(); ...}

Page 50: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Implicit Polymorphism

50

tx = session.beginTransaction();session.createQuery("from com.example.Identifiable i" + “ where i.id = 7“) .list();tx.commit();

...

select [columns] from beer beer0_ where (beer0_.id=7)

select [columns] from trucks truck0_ where (truck0_.id=7)

Unmapped!

Page 51: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Implicit Limitations

51

• No Polymorphic Relationships

– Sort of...

• No “auto-import”

• Query-Per-Type

Page 52: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

Open Source in the Corporate World

Hibernate Interceptor

Overriding Behavior

Page 53: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Hibernate Interceptor

• Extension/Override Points

– Lifecycles

– Transactions

– Identity Queries

• 3.0 adds event system

53

Page 54: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Componentized Domain

Truck

driverNamecargoload(..)unload()

AutoPilotTruck

pilot

ConfigurableAutoPilot

setModel(String)

AutoPilot

model: String

Persistent Entities

Component Interface

Component Implementation

54

Page 55: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

We Have AutoPilot!

55

public class AutoPilotTruck extends Truck { private AutoPilot pilot; // Still need no-arg ctor public AutoPilotTruck() {}

public AutoPilotTruck(AutoPilot pilot) { this.pilot = pilot; }

public String getDriverName() { return pilot.getModel(); } public void setDriverName(String name) { throw new UnsupportedOperationException(); }}

Page 56: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Umh, What Now?

56

• AutoPilotTruck depends on a Component

– The AutoPilot!

• Where do we get the AutoPilot?

Spring is popular, let’s try that!

Page 57: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Spring Configuration

57

<beans default-autowire="constructor" > <bean class="com.example.Truck" singleton="false" /> <bean class="com.example.DomesticBeer" singleton="false" /> <bean class="com.example.ImportedBeer" � singleton="false" />

<bean class="com.example.AutoPilotTruck" singleton="false" />

<bean class="com.example.ConfigurablePilot" singleton="true"> <property name="model"> <value>Wombat.com SuperPilot</value> </property> </bean></beans>

Page 58: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

In the Interceptor

58

class FactoryInterceptor implements Interceptor { private BeanFactory factory; ...

public Object instantiate(String name, EntityMode mode, Serializable id) { Identifiable object = (Identifiable) this.factory.getBean(name);

object.setId(id); return object; }

..

}

Page 59: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

So What?

59

• Benefits

– Container Instantiates Entities

– Resolves Component Dependencies

• Drawbacks

– Still need no-arg constructor

– Complexity

• Neutral

– Only one Interceptor per session

Page 60: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

Open Source in the Corporate World

Filters

Hibernate 3.0’s Killer Feature

Page 61: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

The Problem

• Smart Data Filtering

• Row Level Security

• Context Sensitive Data

– Which applies across types

– Which varies across types

• Separating Concerns

61

Page 62: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Using Filters

62

// 4 beers all in same truck// Prices .75, .50, 4.00, and 5.00 respectivelyassertEquals(4, Tools.countRows("beer"));

session.enableFilter("expensive") .setParameter("price", new BigDecimal("2.00"));

List beer = session.createQuery("from Beer").list();assertEquals(2, beer.size());

Truck t = (Truck) session.createQuery("from Truck") .uniqueResult();assertEquals(2, t.getCargo().size());

Page 63: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Defining Filters

63

<hibernate-mapping> <filter-def name="expensive"> <filter-param name="price" type="java.math.BigDecimal"/> </filter-def></hibernate-mapping>

Page 64: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Filtering Queries

64

<class name="com.example.Beer" .. > ... <filter name="expensive" condition="price > :price" /></class>

Page 65: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Filtering Collections

65

<class name="com.example.Truck" .. > ... <bag name="cargo" cascade="persist"> <key column="truck_id" /> <one-to-many class="com.example.Beer" /> <filter name="expensive" condition="price > :price"/> </bag> ...</class>

Page 66: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Using Filters (Again)

66

// 4 beers all in same truck// Prices .75, .50, 4.00, and 5.00 respectivelyassertEquals(4, Tools.countRows("beer"));

session.enableFilter("expensive") .setParameter("price", new BigDecimal("2.00"));

List beer = session.createQuery("from Beer").list();assertEquals(2, beer.size());

Truck t = (Truck) session.createQuery("from Truck") .uniqueResult();assertEquals(2, t.getCargo().size());

Page 67: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Filter Gotchas

• Filters are applied at SQL level

– Cannot add joins

– Cannot reference relations easily

• Careful with Collections

– Apply to collections as well as types

• Session Cache

– If collection is in Session unfiltered, it remains there

67

Page 68: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

Open Source in the Corporate World

Performance Tuning

The Very Basics

Page 69: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Performance Tuning

• Load Strategies

– Lazy, Eager, Fetch

• Grokking the Cache

– Level 1, Level 2

– Query Cache

– Distributed Cache

69

Page 70: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Load Strategies

• Lazy Load by Default

– It is the default in 3.0, yea!

• Queries are Different

• Join on object references

• Join on one collection

• Beware the Detach

70

Page 71: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Fetches and Queries

71

<class name="com.example.Beer" ... > ... <many-to-one name="truck" column="truck_id" fetch="join" /></class>

...

tx = session.beginTransaction();Beer beer = (Beer) session.createQuery("from Beer") .uniqueResult();Truck t = beer.getTruck();assertFalse(Hibernate.isInitialized(t));t.getDriverName();assertTrue(Hibernate.isInitialized(t));tx.commit();

Page 72: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Fetches and Queries

72

tx = session.beginTransaction();

Beer beer = (Beer) session .createQuery("from Beer b join fetch b.truck") .uniqueResult();

Truck t = beer.getTruck();

assertTrue(Hibernate.isInitialized(t));

tx.commit();

Page 73: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Where Fetch Applies

73

• Proxy Loading

• Session#get

• Session#load

Page 74: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Caching

• First Level Cache

• Second Level Cache

• Query Cache

• Distributed Caches

74

Page 75: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

First Level Cache

• The Session

• Everything loaded by this session

75

Page 76: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Second Level Cache

76

<class-cache class="com.example.Truck" usage="read-write"/><collection-cache collection="com.example.Truck.cargo" usage="nonstrict-read-write"/>

<class name="com.example.Truck" ... > <cache usage="read-write" /> <bag name="cargo" cascade="persist" inverse="false"> <cache usage="read-write"/> <key column="truck_id" /> <one-to-many class="com.example.Beer" /> </bag></class>

hibernate.cfg.xml

Truck.hbm.xml

Page 77: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Second Level Cache

77

• Caches flat data

• Entities and Collections are separate

• Collections require Transactional

• Configuration and Mapping flexibility

Page 78: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Query Cache

78

<hibernate-configuration> <session-factory> <property name="cache.use_query_cache"> true </property> ... </session-factory></hibernate-configuration>

session.createQuery("select b from Beer b " + " join fetch b.truck " + "where " + " b.truck.driverName = :name") .setCacheable(true) .setString("name", "George") .list();

hibernate.cfg.xml

Using it...

Page 79: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Query Cache

79

• Caches ID’s for query results

• Uses second level cache to materialize

• Issues query for the rest

Page 80: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Usage

80

• read-only

– Most performant

• nonstrict-read-write

– If low risk/cost of collision or dirty read

• read-write

– No serializable tx, Beware of clusters

• transactional

– Safest, works correctly with transactions

Page 81: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

Clusters

• Local Caching in Clusters

• Distributed Caching in Clusters

• Small

– JBoss TreeCache, SwarmCache, OSCache, ActiveSpace

• Large

– Memcached*, Commercial

81

* Custom Provider

Page 82: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

Open Source in the Corporate World

Questions?

Page 83: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

HibernateCopyright Chariot Solutions 2005

What We Looked At

• Super Quick Intro

• Thinking in Object Graphs

• Transactions (so Overloaded!)

• Inheritance and Polymorphism

• Hibernate Interceptor

• Filters

• Performance Tuning

83

Page 84: Hands-On Hibernatechariotsolutions.com/.../276/osconf2005-hibernate.pdf · Open Source in the Corporate World Super Quick Intro Don’t Blink!

Open Source in the Corporate World

Thank You!

http://www.chariotsolutions.com/