How Hibernate3 makes complex things easy

14 downloads 97 Views 136KB Size Report
The problems. • Hibernate 2.1 assumes that the data model is “nice” ... A null association has a null foreign key (except for one-to-one ... association mapping. ✓.
How Hibernate3 makes (some) complex things easy Gavin King JBoss, Inc [email protected] [email protected]

The Professional Open Source™ Company

Road Map • • • • • •

The problems The model Using union-subclass mappings A typed association Using filters Producing XML

The Professional Open Source™ Company

The problems •

Hibernate 2.1 assumes that the data model is “nice” 9 9 9



ORM solutions assume that the model is essentially static 9 9



The data changes over time, but… …at any particular time, all users see the same data

If either of these assumptions fails, you can’t use an association mapping 9



Relationships have foreign key constraints Foreign keys point to primary keys A null association has a null foreign key (except for one-to-one special case)

You’ll need to use a query (HQL, or possibly SQL) to discover the relationships in your data

This is a pain for 9 9

Many legacy databases Databases with historical, regional, permissioned data

The Professional Open Source™ Company

The model • A hypothetical legacy insurance application • Parties to a contract (policy holders, others) are people or organizations • Addresses have a “role” (mailing, business, home) Identity

1

mailing home

Address

1 business 1

Person

Organization

The Professional Open Source™ Company

The model • There are various standard policy definitions which change over time • The terms of a contract (a policy variation) may also be varied over time

Identity

1

1

Party

*

*

PolicyVariation

* 1

PolicyDefinition

The Professional Open Source™ Company

Union-subclass mappings • What if there is no Identity table? • We have a one-to-one association between the Party and Identity classes 9

But it has no foreign key constraint

• In Hibernate3, use a mapping

The Professional Open Source™ Company

Union-subclass mapping … … …

The Professional Open Source™ Company

Union-subclass mapping • Queries against this class result in a SQL union

session.createQuery(“from Party p left join fetch p.identity”).list(); select … from Party p left join (select … from Person union select … from Organization) i on p.id = i.id

The Professional Open Source™ Company

Typed association • There are three optional associations from the Identity hierarchy to the Address class • But in the database, this is represented as the Address table having a foreign key value of the Organization or Person table, together with a “type” • So, in the data model, we have a one-to-many association, while in the object model, we have three optional one-to-one associations

The Professional Open Source™ Company

Typed association public class Address implements java.io.Serializable { public enum AddressType { MAILING, HOME, BUSINESS } private private private private private private

Identity addressee; AddressType type; String address; String state; String postCode; String country;

… } create table Address ( identityId bigint not null, addressType varchar(10) not null check addressType in (‘MAILING’, ‘BUSINESS’, ‘HOME), street varchar(100) not null, city varchar(30) not null, … primary key (identityId, addressType) ) The Professional Open Source™ Company

Typed association …

The Professional Open Source™ Company

Using filters • There is a many-to-many association between Party and PolicyVariation



The Professional Open Source™ Company

Using filters • And a one-to-many association between PolicyDefinition and PolicyVariation

… …

The Professional Open Source™ Company

Using filters • Also, apply to the collection-valued associations …