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:

Slides:



Advertisements
Similar presentations
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Advertisements

Data Design The futureERD - CardinalityCODINGRelationshipsDefinition.
Accounting System Design
Concepts of Database Management Sixth Edition
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 The Basic (Flat) Relational Model.
Entities and Persistence. Entity Beans Topics to be Covered: Entities are POJOs Managed/Unmanaged Entities Persistence Unit EntityManager Basic Relational.
RELATIONSHIP  THE WAY TABLES ARE RELATED  A TABLE MUST PARTICIPATE IN AT LEAST ONE RELATIONSHIP  IN A BINARY RELATIONSHIP TWO ENTITIES PARTICIPATE 
1 © Prentice Hall, 2002 Chapter 5: Logical Database Design and the Relational Model Modern Database Management 6 th Edition Jeffrey A. Hoffer, Mary B.
Accounting Databases Chapter 2 The Crossroads of Accounting & IT
Chapter 5 The Relational Database Model: Introduction
1 ODB Design: Handling Associations and Inheritance in ODL M. Akhtar Ali School of Informatics.
CSE446 S OFTWARE Q UALITY M ANAGEMENT Spring 2014 Yazılım ve Uyguluma Geliştirme Yöneticisi Orhan Başar Evren.
Database Design Concepts
Module Title? DBMS E-R Model to Relational Model.
Maven for building Java applications By Nalin De Zoysa
1 Lecture 17 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES.
1 The Seven Relationship Types Seven types of relationships can exist between entity beans. There are four types of cardinality: one-to- one, one-to-many,
JPA Java Persistence API. Introduction The Java Persistence API provides an object/relational mapping facility for managing relational data in Java applications.
Part 06 – A More Complex Data Model Entity Framework and MVC NTPCUG Tom Perkins.
Concepts and Terminology Introduction to Database.
RELATIONSHIPS Generally there are two main database types: flat-file and relational.
Geodatabase Relationships & Relationship Classes GIS 458 Spring 2006 Larry Clamp.
CS 3630 Database Design and Implementation. 2 Mathematical Relation A mathematical relation is a subset of a Cartesian Product. A1  A2  A3  …  An.
PLUG IT IN 3 Fundamentals of Relational Database Operations.
© 2008 The McGraw-Hill Companies, Inc. All rights reserved. ACCESS 2007 M I C R O S O F T ® THE PROFESSIONAL APPROACH S E R I E S Lesson 9 – Building Links,
CS 157B: Database Management Systems II February 4 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak.
CSCI 3140 Module 3 – Logical Database Design for the Relational Model Theodore Chiasson Dalhousie University.
1 Mathematical Relation A mathematical relation is a subset of a Cartesian Product. A1  A2  A3  …  An = {(x1, x2, x3, …, xn): xi  Ai} R  A1  A2.
Topic : JPA Kaster Nurmukan. Overview of JPA EntityManager.
EJB 3.0 Persistence Based on: Patel, Brose, Silverman, Mastering Enterprise JavaBeans 3.0.
Microsoft Access Intro Class 6 Relationships.
Slide Chapter 5 The Relational Data Model and Relational Database Constraints.
Java Data Persistence Using Hibernate Jack Gardner October 2004.
1 © Prentice Hall, 2002 Chapter 5: Logical Database Design and the Relational Model Modern Database Management 6 th Edition Jeffrey A. Hoffer, Mary B.
Access Review. Access Access is a database application A database is a collection of records and files organized for a particular purpose Access supports.
Jozef Kuper.  Describe a Database  Entities  Atributes  Relationships.
Burapha University, 2003 Object-Oriented Analysis Basic of Object Mapping to Relational Database.
Cloudacus = LAMP + Appaserver ©Tim Riley Appahost Model.
Middleware Technology (J2EE/EJB) Entity Bean (JBoss EJB 3.0 tutorial)
ENTITY RELATIONSHIP DIAGRAM ENTITY RELATIONSHIP DIAGRAM IS A SPECIALIZED GRAPHIC THAT ILLUSTRATES THE INTERRELATIONSHIPS BETWEEN ENTITIES IN A DATABASE.
Topic : Hibernate 3:Advanced ORM Kaster Nurmukan.
Chapter 9 Logical Database Design : Mapping ER Model To Tables.
Enterprise Java v041109Container Managed Relationships1 Container Managed Relationships (CMR) Source: “Enterprise JavaBeans, 3rd Edition”, Richard Monson-Haefel.
CSE314 Database Systems Lecture 3 The Relational Data Model and Relational Database Constraints Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
Copyright © 2013 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill/Irwin APPENDIX C DESIGNING DATABASES APPENDIX C DESIGNING DATABASES.
1 ER Modeling BUAD/American University Mapping ER modeling to Relationships.
The Java Persistence API ©SoftMoore ConsultingSlide 1.
Java Persistence API part 1 INFORMATICS ENGINEERING – UNIVERSITY OF BRAWIJAYA Eriq Muhammad Adams J
Java Persistence API (JPA) Relationships. Kinds of relationships UML associations and aggregations (ER non- identifying relationships, shared semantics)
Mapping Objects ↔Relational DB. The Problem with Databases Databases store data in rows in tables, which are not like objects. We can simulate object.
Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February
Logical Database Design and the Relational Model.
Chapter 14 Semantic Modeling. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.14-2 Topics in this Chapter The Overall Approach The E/R Model.
Builder Pattern. What’s Builder: TO find a solution to the telescoping constructor Problem: Too many parameters Solution: To get an abstract object, a.
1 ODB Design Handling Inheritance in ODL M. Akhtar Ali School of Informatics.
Hibernate Thuy, Le Huu. Pentalog VN. Agenda Hibernate Annotations Improving performance – Lazy loading – Fetching Strategies – Dynamic insert, dynamic.
CS 3630 Database Design and Implementation. 2 Mathematical Relation A mathematical relation is a subset of a Cartesian Product. A1  A2  A3  …  An.
Teanna Clarke 6aQ What is a Entity-Relationship Diagram?  An Entity-Relationship Diagram (ERD) is a tool that graphically shows the connections among.
Data Modeling AND ER MODELS.
CPSC 252 Tables / Maps / Dictionaries Page 1 Tables, Maps and Dictionaries A table (or map or dictionary) is a collection of key/value pairs. In general.
CS520 Web Programming Object-Relational Mapping with Hibernate and JPA Chengyu Sun California State University, Los Angeles.
CS520 Web Programming Object-Relational Mapping with Hibernate and JPA (I) Chengyu Sun California State University, Los Angeles.
Hibernate Annotation 李日貴 (jini) jakarta99 AT gmail.com SoftLeader Tech. Corp. Taiwan Java Annotation Lesson 1.
XP Chapter 1 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Level 2 Objectives: Understanding and Creating Table.
CS6320 – Java Persistence API
Chengyu Sun California State University, Los Angeles
Advanced Java Programming
Chapter 12 Information Systems.
JPA: Mapping to an existing database
Chengyu Sun California State University, Los Angeles
Presentation transcript:

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: –Unidirectional –Bidirectional One-to-Many and Many-to-One bidirectional relationships are identical

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

One-to-One Unidirectional Relationship

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

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

One-to-One Unidirectional – Entity public class Customer implements java.io.Serializable {... private public Address getAddress() { return address; } public void setAddress(Address addr) { this.address = addr; }

One-to-One Bidirectional Relationship

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

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

One-to-One Bidirectional – Entity public class Customer implements java.io.Serializable {... private public CreditCard getCreditCard() { return credCard; } public void setCreditCard(CreditCard cc) { this.credCard = cc; }

One-to-One Bidirectional – Entity public class CreditCard implements java.io.Serializable {... private Customer public Customer getCustomer() { return customer; } public void setCustomer(Customer cust) { this.customer = cust; }

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

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);

One-to-Many Unidirectional Relationship

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

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)

One-to-Many Unidirectional – Entity public class Customer implements java.io.Serializable {... private Collection phones = new public Collection getPhones() { return phones; } public void setPhones(Collection p) { this.phones = p; }

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

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

One-to-Many Unidirectional – Join Table public class Customer implements java.io.Serializable {... private Collection phones = new public Collection getPhones() { return phones; } public void setPhones(Collection p) { this.phones = p; }

Many-to-One Unidirectional Relationship

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*

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

Many-to-One Unidirectional – Entity public class Flight implements java.io.Serializable {... private public Plane getPlane() { return plane; } public void setPlane(Plane pl) { this.plane = pl; }

One-to-Many Bidirectional Relationship

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*

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

One-to-Many Bidirectional – Entity public class Reservation implements java.io.Serializable {... private public Flight getFlight() { return flight; } public void setFlight(Flight fl) { this.flight = fl; }

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

Many-to-Many Bidirectional Relationship

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 **

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

Many-to-Many Bidirectional – Entity public class Reservation implements java.io.Serializable {... private Set custs = new public Set getCust() { return custs; } public void setCust(Set cu) { this.custs = cu; }

Many-to-Many Bidirectional – Entity public class Customer implements java.io.Serializable {... private Collection res = new ArrayList public Collection getRes() { return res; } public void setRes(Collection r) { this.res = r; }

Many-to-Many Unidirectional Relationship

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 **

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

Many-to-Many Unidirectional – Entity public class Reservation implements java.io.Serializable {... private Set planeSeats = new public Set getSeats() { return planeSeats; } public void setSeats(Set ps) { this.planeSeats = ps; }

Cascading

cascade @ManyToOne, relationship

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);

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 }

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

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

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