Presentation is loading. Please wait.

Presentation is loading. Please wait.

Entity Relationships. Seven Relationship Types Four types of cardinality: –One-to-One –One-to-Many –Many-to-One –Many-to-Many Each relationship can be:

Similar presentations


Presentation on theme: "Entity Relationships. Seven Relationship Types Four types of cardinality: –One-to-One –One-to-Many –Many-to-One –Many-to-Many Each relationship can be:"— Presentation transcript:

1 Entity Relationships

2 Seven Relationship Types Four types of cardinality: –One-to-One –One-to-Many –Many-to-One –Many-to-Many Each relationship can be: –Unidirectional –Bidirectional One-to-Many and Many-to-One bidirectional relationships are identical

3 Relationships In order to model real-world business concepts, entity beans must be capable of forming complex relationships –Examples: A Customer entity can have a one-to-one relationship with an Address entity A Customer entity can have a one-to-many relationship with a Phone entity Represent the navigability of the domain model Specified by applying annotations to related entity beans

4 One-to-One Unidirectional Relationship

5 One-to-One Unidirectional Relationship Customer + getLastName() : String + setLastName(In : String) : void + getFirstName() : String + setFirstName(In : String) : void + getAddress() : Address + setAddress(In : Address) : void Address + getStreet() : String + setStreet(In : String) : void + getCity() : String + setCity(In : String) : void … 11

6 One-to-One Unidirectional – Relational Database Schema CUSTOMER table contains a foreign key to the ADDRESS table ADDRESS table does not contain a foreign key to the CUSTOMER table

7 One-to-One Unidirectional – Entity Annotation @Entity public class Customer implements java.io.Serializable {... private Address address;... @OneToOne(cascade={CascadeType.ALL}) @JoinColumn(name=“ADDRESS_ID”) public Address getAddress() { return address; } public void setAddress(Address addr) { this.address = addr; }

8 One-to-One Bidirectional Relationship

9 One-to-One Bidirectional Relationship Customer + getLastName() : String + setLastName(In : String) : void + getFirstName() : String + setFirstName(In : String) : void + getCreditCard() : CreditCard + setCreditCard(In : CreditCard) : void CreditCard + getNumber() : String + setNumber(In : String) : void + getExpiration() : Date + setExpiration(In : Date) : void + getCustomer() : Customer + setCustomer(In : Customer) : void 11

10 One-to-One Bidirectional – Relational Database Schema CUSTOMER table contains a foreign key to the CREDIT_CARD table CREDIT_CARD table does not contain a foreign key to the CUSTOMER table Bidirectional relationship established and maintained by the application programmer, not the persistence provider

11 One-to-One Bidirectional – Entity Annotation @Entity public class Customer implements java.io.Serializable {... private CreditCard credCard;... @OneToOne(cascade={CascadeType.ALL}) @JoinColumn(name=“CREDIT_CARD_ID”) public CreditCard getCreditCard() { return credCard; } public void setCreditCard(CreditCard cc) { this.credCard = cc; }

12 One-to-One Bidirectional – Entity Annotation @Entity public class CreditCard implements java.io.Serializable {... private Customer customer;... @OneToOne(mappedBy=“creditCard”) public Customer getCustomer() { return customer; } public void setCustomer(Customer cust) { this.customer = cust; }

13 One-to-One Bidirectional – Establishing the Relationship Customer cust = new Customer(); CreditCard card = new CreditCard(); cust.setCreditCard(card); card.setCustomer(cust);

14 One-to-One Bidirectional – Modifying the Relationship Customer newCust = em.find(Customer.class, newCustId); CreditCard card = oldCustomer.getCreditCard(); oldCustomer.setCreditCard(null); newCust.setCreditCard(card); card.setCustomer(newCust);

15 One-to-Many Unidirectional Relationship

16 One-to-Many Unidirectional Relationship Customer + getLastName() : String + setLastName(In : String) : void + getFirstName() : String + setFirstName(In : String) : void + getPhones() : Collection + setPhones(In : Collection ) : void Phone + getNumber() : String + setNumber(In : String) : void + getType() : String + setType(In : String) : void *1

17 One-to-Many Unidirectional – Relational Database Schema Could be mapped with a join table or: PHONE table contains a foreign key to the CUSTOMER table (reverse pointer scenario)

18 One-to-Many Unidirectional – Entity Annotation @Entity public class Customer implements java.io.Serializable {... private Collection phones = new ArrayList ();... @OneToMany(cascade={CascadeType.ALL}) @JoinColumn(name=“CUSTOMER_ID”) public Collection getPhones() { return phones; } public void setPhones(Collection p) { this.phones = p; }

19 One-to-Many Unidirectional – Establishing the Relationship Customer cust = em.find(Customer.class, pk); Phone ph = new Phone(“617-333-3333”, 5); cust.getPhones().add(ph);

20 One-to-Many Unidirectional – Removing the Relationship cust.getPhones().remove(ph); em.remove(ph);

21 One-to-Many Unidirectional – Join Table Mapping @Entity public class Customer implements java.io.Serializable {... private Collection phones = new ArrayList ();... @OneToMany(cascade={CascadeType.ALL}) @JoinTable(name=“CUSTOMER_PHONE”, joinColumns={@JoinColumn(name=“CUSTOMER_ID”)}, inverseJoinColumns={@JoinColumn(name=“PHONE_ID”)}) public Collection getPhones() { return phones; } public void setPhones(Collection p) { this.phones = p; }

22 Many-to-One Unidirectional Relationship

23 Many-to-One Unidirectional Relationship Flight + getNumber() : int + setNumber(In : int) : void + getPlane() : Plane + setPlane(In : Plane) : void Plane + getModel() : String + setModel(In : String) : void … 1*

24 Many-to-One Unidirectional – Relational Database Schema FLIGHT table contains a foreign key to the PLANE table

25 Many-to-One Unidirectional – Entity Annotation @Entity public class Flight implements java.io.Serializable {... private Plane plane;... @ManyToOne @JoinColumn(name=“PLANE_ID”) public Plane getPlane() { return plane; } public void setPlane(Plane pl) { this.plane = pl; }

26 One-to-Many Bidirectional Relationship

27 One-to-Many Bidirectional Relationship Reservation + getNumber() : int + setNumber(In : int) : void + getRes() : Collection + setRes(In : Collection ) : void Flight + getDate() : Date + setDate(In : Date) : void + getFlight() : Flight + setFlight(In : Flight) : void … 1*

28 One-to-Many Bidirectional – Relational Database Schema RESERVATION table contains a foreign key to the FLIGHT table

29 One-to-Many Bidirectional – Entity Annotation @Entity public class Reservation implements java.io.Serializable {... private Flight flight;... @ManyToOne @JoinColumn(name=“FLIGHT_ID”) public Flight getFlight() { return flight; } public void setFlight(Flight fl) { this.flight = fl; }

30 One-to-Many Bidirectional – Entity Annotation @Entity public class Flight implements java.io.Serializable {... private Collection res = new ArrayList ();... @OneToMany(mappedBy=“flight”) public Collection getRes() { return res; } public void setRes(Collection r) { this.res = r; }

31 Many-to-Many Bidirectional Relationship

32 Many-to-Many Bidirectional Relationship Reservation + getName() : String + setName(In : String) : void + getRes() : Collection + setRes(In : Collection ) : void Customer + getDate() : Date + setDate(In : Date) : void + getCust() : Collection + setCust(In : Collection ) : void **

33 Many-to-Many Bidirectional – Relational Database Schema Use a RESERVATION_CUSTOMER association table

34 Many-to-Many Bidirectional – Entity Annotation @Entity public class Reservation implements java.io.Serializable {... private Set custs = new HashSet ();... @ManyToMany @JoinTable(name=“RESERVATION_CUSTOMER”), joinColumns={@JoinColumn(name=“RESERVATION_ID”)}, inverseJoinColumns={@JoinColumn(name=“CUSTOMER_ID”)}) public Set getCust() { return custs; } public void setCust(Set cu) { this.custs = cu; }

35 Many-to-Many Bidirectional – Entity Annotation @Entity public class Customer implements java.io.Serializable {... private Collection res = new ArrayList ();... @ManyToMany(mappedBy=“cust”) public Collection getRes() { return res; } public void setRes(Collection r) { this.res = r; }

36 Many-to-Many Unidirectional Relationship

37 Many-to-Many Unidirectional Relationship Reservation + getLocation() : String + setLocation(In : String) : void PlaneSeat + getDate() : Date + setDate(In : Date) : void + getSeats() : Collection + setSeats(In : Collection ) : void **

38 Many-to-Many Unidirectional – Relational Database Schema Use a RESERVATION_PLANESEAT association table

39 Many-to-Many Unidirectional – Entity Annotation @Entity public class Reservation implements java.io.Serializable {... private Set planeSeats = new HashSet ();... @ManyToMany @JoinTable(name=“RESERVATION_PLANESEAT”), joinColumns={@JoinColumn(name=“RESERVATION_ID”)}, inverseJoinColumns={@JoinColumn(name=“PLANESEAT_ID”)}) public Set getSeats() { return planeSeats; } public void setSeats(Set ps) { this.planeSeats = ps; }

40 Cascading

41 cascade Attribute of @OneToOne, @OneToMany, @ManyToOne, and @ManyToMany relationship annotations @OneToOne(cascade={CascadeType.ALL})

42 Cascading When you preform an entity manager operation on an entity, you can automatically have the same operation performed on any relationship properties the entity may have Example: Customer entity has one-to-one relationship with Address entity and one-to-many relationship with Phone entity Customer cust = new Customer(); cust.setAddress(new Address()); cust.getPhones().add(new Phone()); // create all in one entity manager invocation entityManager.persist(cust);

43 Applying Cascading Cascading can be applied to a variety of entity manager operations using javax.persistence.CascadeType : public enum CascadeType { ALL, PERSIST, MERGE, REMOVE, REFRESH }

44 Cascade Types PERSIST –Inserts records of related objects MERGE –Inserts and updates records of related objects REMOVE –Removes records of related objects REFRESH –Refreshes related object from the database ALL –Combination of all of the above

45 When to Use Cascading Not always applicable –Example: Do not want to remove a related Flight when removing a Reservation Cascading simply a convenience tool for reducing the number of EntityManager API calls

46 Seven Relationship Types Four types of cardinality: –One-to-One –One-to-Many –Many-to-One –Many-to-Many Each relationship can be: –Unidirectional –Bidirectional One-to-Many and Many-to-One bidirectional relationships are identical


Download ppt "Entity Relationships. Seven Relationship Types Four types of cardinality: –One-to-One –One-to-Many –Many-to-One –Many-to-Many Each relationship can be:"

Similar presentations


Ads by Google