Download presentation
Presentation is loading. Please wait.
Published byLynette Page Modified over 9 years ago
1
Topic : Hibernate 3:Advanced ORM Kaster Nurmukan
2
O/R Mapping Primary entity annotations EntityManager Entity Relationships Practice
3
Class-level annotations –@Entity and @Table Id field –@Id and @GeneratedValue Fields of simple types –@Basic (can be omitted) and @Column Fields of class types –@ManyToOne and @OneToOne
4
Embedded class Collections Inheritance
5
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; }
6
@Embeddable public class Address { String street; String city; String state; String zip; } @Entity public class User { @Id Integer id; String username String password; @Embedded Address address; }
7
public class Customer { Integer id; String name; String address; Set phones; }
8
id customersCustomer_phones Customer_idphones @ElementCollection Set phones;
9
@ElementCollection @CollectionTable( name = “customer_phones”, joinColumns=@JoinColumn(name = “customer_id”) ) @Column(name=“phone”) Set phones;
10
Order by property –@OrderBy(“ ASC|DESC”) –Simple types do not have properties Order by a separate column @ElementCollection @OrderBy(“asc”) List phones; @ElementCollection @OrderColumn(name = “phone_order”) List phones;
11
Relationships (a.k.a. associations) –one-to-many –many-to-many Unidirectional vs. Bidirectional Set and List Cascading behaviors
12
Many-to-Many Many-to-One / One-to-Many One-to-One
13
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
14
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
15
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
16
Books and authors?? Books and editors??
17
A customer may own multiple accounts An account only has one owner
18
public class Account { Integer id; Double balance; Date createdOn; Customer owner; } public class Customer { Integer id; String name; String address; Set phones; Set accounts; }
19
public class Account { Integer id; Double balance; Date createdOn; } public class Customer { Integer id; String name; String address; Set phones; Set accounts; }
20
public class Account { Integer id; Double balance; Date createdOn; Customer owner; } public class Customer { Integer id; String name; String address; Set phones; }
21
Do the three OO designs result in different database schemas?? Does it make any difference on the application side?? Which one should we use??
22
public class Account { Integer id; Double balance; Date createdOn; @ManyToOne Customer owner; } public class Customer { Integer id; String name; String address; Set phones; @OneToMany(mappedBy=“owner”) Set accounts; } property
23
public class Customer { Integer id; String name; String address; Set phones; @OneToMany(mappedBy=“owner”) @OrderBy( “createdOn asc” ) List accounts; }
24
A customer may own multiple accounts An account may have multiple owners
25
public class Account { Integer id; Double balance; Date createdOn; @ManyToMany Set owners; } public class Customer { Integer id; String name; String address; Set phones; @ManyToMany(mappedBy=“owners”) Set accounts; }
26
@ManyToMany @JoinTable( name = “account_owners”, joinColumns=@JoinColumn(name = “account_id”), inverseJoinColumns=@JoinColumn(name=“owner_id”) ) Set owners;
27
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??
28
http://sun.calstatela.edu/~cysun/documentation/jpa-2.0- api/javax/persistence/CascadeType.htmlhttp://sun.calstatela.edu/~cysun/documentation/jpa-2.0- api/javax/persistence/CascadeType.html
29
@OneToMany(mappedBy=“owner”, cascade=CascadeType.PERSIST) List accounts; @OneToMany(mappedBy=“owner”, cascade={CascadeType.PERSIST, CascadeType.MERGE}) List accounts; @OneToMany(mappedBy=“owner”, cascade=CascadeType.ALL) List accounts;
30
public class CDAccount extends Account { Integer term; }
31
idaccount_typebalancecreated_onterm accounts Discriminator column
32
@Entity @Table(name=“accounts”) @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name=“account_type”) @DiscrimnatorValue(“CHECKING”) public class Account { … } @Entity @DiscrimnatorValue(“CD”) public class CDAccount { … }
33
idbalancecreated_on accounts account_idterm cd_accounts foreign key
34
@Entity @Table(name=“accounts”) @Inheritance(strategy=InheritanceType.JOINED) public class Account { … } @Entity @Table(name=“cd_accounts”) public class CDAccount { … }
35
idbalancecreated_on accounts idbalancecreated_onterm cd_accounts
36
@Entity @Table(name=“accounts”) @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS) public class Account { … } @Entity @Table(name=“cd_accounts”) public class CDAccount { … }
37
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
38
http://www.oracle.com TopLink JPA Annotation Reference – http://www.oracle.com/technetwork/middleware/ias/toplink-jpa- annotations-096251.html http://www.oracle.com/technetwork/middleware/ias/toplink-jpa- annotations-096251.html Pro JPA 2 by Mike Keith and Merrick Schincariol
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.