Topic : Hibernate 3:Advanced ORM Kaster Nurmukan
O/R Mapping Primary entity annotations EntityManager Entity Relationships Practice
Class-level annotations Id field Fields of simple types (can be omitted) Fields of class types
Embedded class Collections Inheritance
public class Address { String street; String city; String state; String zip; } users id…streetcitystatezip… public class User { Integer id; String username String password; Address address; }
@Embeddable public class Address { String street; String city; String state; String zip; public class User Integer id; String username String Address address; }
public class Customer { Integer id; String name; String address; Set phones; }
id customersCustomer_phones Set phones;
name = “customer_phones”, = “customer_id”) Set phones;
Order by property ASC|DESC”) –Simple types do not have properties Order by a = “phone_order”) List phones;
Relationships (a.k.a. associations) –one-to-many –many-to-many Unidirectional vs. Bidirectional Set and List Cascading behaviors
Many-to-Many Many-to-One / One-to-Many One-to-One
Many-to-Many Relationship Each entity in E 1 can be related to many entities in E 2 Each entity in E 2 can be related to many entities in E 1 E1E1 E2E2
Many-to-One Relationship Each entity in E 1 can be related to one entities in E 2 Each entity in E 2 can be related to many entities in E 1 E1E1 E2E2
One-to-One Relationship Each entity in E 1 can be related to one entities in E 2 Each entity in E 2 can be related to one entities in E 1 E1E1 E2E2
Books and authors?? Books and editors??
A customer may own multiple accounts An account only has one owner
public class Account { Integer id; Double balance; Date createdOn; Customer owner; } public class Customer { Integer id; String name; String address; Set phones; Set accounts; }
public class Account { Integer id; Double balance; Date createdOn; } public class Customer { Integer id; String name; String address; Set phones; Set accounts; }
public class Account { Integer id; Double balance; Date createdOn; Customer owner; } public class Customer { Integer id; String name; String address; Set phones; }
Do the three OO designs result in different database schemas?? Does it make any difference on the application side?? Which one should we use??
public class Account { Integer id; Double balance; Date Customer owner; } public class Customer { Integer id; String name; String address; Set Set accounts; } property
public class Customer { Integer id; String name; String address; “createdOn asc” ) List accounts; }
A customer may own multiple accounts An account may have multiple owners
public class Account { Integer id; Double balance; Date Set owners; } public class Customer { Integer id; String name; String address; Set Set accounts; }
name = “account_owners”, = “account_id”), ) Set owners;
Whether an operation on the parent object (e.g. Customer) should be applied to the children objects in a collection (e.g. List ) Customer c = new Customer(“cysun”); Account a1 = new Account(); Account a2 = new Account(); c.getAccounts().add( a1 ); c.getAccounts().add( a2 ); entityManager.persist(c); // will a1 and a2 be saved as well? entityManager.remove(c); // will a1 and a2 be deleted from db??
api/javax/persistence/CascadeType.htmlhttp://sun.calstatela.edu/~cysun/documentation/jpa-2.0- api/javax/persistence/CascadeType.html
@OneToMany(mappedBy=“owner”, cascade=CascadeType.PERSIST) List cascade={CascadeType.PERSIST, CascadeType.MERGE}) List cascade=CascadeType.ALL) List accounts;
public class CDAccount extends Account { Integer term; }
idaccount_typebalancecreated_onterm accounts Discriminator column
@DiscrimnatorValue(“CHECKING”) public class Account { public class CDAccount { … }
idbalancecreated_on accounts account_idterm cd_accounts foreign key
@Entity public class Account { public class CDAccount { … }
idbalancecreated_on accounts idbalancecreated_onterm cd_accounts
@Entity public class Account { public class CDAccount { … }
Understand relational design –Know what the database schema should looks like before doing the mapping Understand OO design –Make sure the application design is object-oriented
TopLink JPA Annotation Reference – annotations html annotations html Pro JPA 2 by Mike Keith and Merrick Schincariol