Java Persistence API (JPA) Relationships. Kinds of relationships UML associations and aggregations (ER non- identifying relationships, shared semantics)

Slides:



Advertisements
Similar presentations
Designing tables from a data model (Chapter 6) One base table for each entity.
Advertisements

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 5/1 Copyright © 2004 Please……. No Food Or Drink in the class.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
IT420: Database Management and Organization
Entity-Relationship Model
UML Class Diagram. UML Class Diagrams2 Agenda What is a Class Diagram? Essential Elements of a UML Class Diagram Tips.
8 June Single table database in normal form Fields and records Normal form 1.Header in the first line 2.Same content for every field 3.Each record.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 5-1 COS 346 Day 6.
Chapter 6 Methodology Logical Database Design for the Relational Model Transparencies © Pearson Education Limited 1995, 2005.
Fundamentals, Design, and Implementation, 9/e COS 346 Day 8.
Concepts of Database Management Sixth Edition
Fundamentals, Design, and Implementation, 9/e Chapter 5 Database Design.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 5-1 David M. Kroenke Database Processing Tenth Edition Chapter 5 Data.
Methodology Logical Database Design for the Relational Model
RELATIONSHIP  THE WAY TABLES ARE RELATED  A TABLE MUST PARTICIPATE IN AT LEAST ONE RELATIONSHIP  IN A BINARY RELATIONSHIP TWO ENTITIES PARTICIPATE 
Chapter Five Data Modeling with the Entity-Relationship Model.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 5-1 COS 346 Day 6.
Transforming Data Models into Database Designs
WJEC Applied ICT Databases – Attributes & Entities Entities A database contains one or more related tables. Each table holds all of the information.
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.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 5-1 David M. Kroenke’s Chapter Five: Data Modeling with the Entity-Relationship.
DeSiamorewww.desiamore.com/ifm1 Database Management Systems (DBMS)  B. Computer Science and BSc IT Year 1.
Module Title? DBMS E-R Model to Relational Model.
OBJECT ORIENTED PROGRAMMING LECTURE 12 Instructor: Rashi Garg Coordinator: Gaurav Saxena.
Maven for building Java applications By Nalin De Zoysa
JPA Java Persistence API. Introduction The Java Persistence API provides an object/relational mapping facility for managing relational data in Java applications.
IS-907 Java EE JPA: Simple Object-Relational Mapping.
1 ER Modeling BUAD/American University Entity Relationship (ER) Modeling.
MIS 301 Information Systems in Organizations Dave Salisbury ( )
Introduction to Entities
Object Oriented Analysis & Design & UML (Unified Modeling Language)1 Part V: Design The Design Workflow Design Classes Refining Analysis Relationships.
MS Access: Creating Relational Databases Instructor: Vicki Weidler Assistant: Joaquin Obieta.
© 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,
Optimize Your Object Model Paul Dayan – Sales Engineer.
Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.
CSCI 3140 Module 3 – Logical Database Design for the Relational Model Theodore Chiasson Dalhousie University.
Object Oriented Analysis and Design 1 Chapter 7 Database Design  UML Specification for Data Modeling  The Relational Data Model and Object Model  Persistence.
Concepts of Database Management Sixth Edition Chapter 6 Database Design 2: Design Method.
EJB 3.0 Persistence Based on: Patel, Brose, Silverman, Mastering Enterprise JavaBeans 3.0.
DataBase Management System What is DBMS Purpose of DBMS Data Abstraction Data Definition Language Data Manipulation Language Data Models Data Keys Relationships.
DeSiamorePowered by DeSiaMore1 Database Management Systems (DBMS)  B. Computer Science and BSc IT Year 1.
Msigwaemhttp//:msigwaem.ueuo.com/1 Database Management Systems (DBMS)  B. Computer Science and BSc IT Year 1.
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.
Concepts of Database Management, Fifth Edition Chapter 6: Database Design 2: Design Methodology.
Description and exemplification of entity-relationship modelling.
In this session, you will learn to: Map an ER diagram to a table Objectives.
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.
CSE314 Database Systems Lecture 3 The Relational Data Model and Relational Database Constraints Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
1 ER Modeling BUAD/American University Mapping ER modeling to Relationships.
Java Persistence API part 1 INFORMATICS ENGINEERING – UNIVERSITY OF BRAWIJAYA Eriq Muhammad Adams J
CLASS DIAGRAMS A classy approach to objects. The Basic Class Diagram  Class Name  Attributes (- indicates private each would have properties or accessor/mutator.
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.
CSE 412/598 DATABASE MANAGEMENT COURSE NOTES 3. ENTITY-RELATIONSHIP CONCEPTUAL MODELING Department of Computer Science & Engineering Arizona State University.
1 6 Concepts of Database Management, 5 th Edition, Pratt & Adamski Chapter 6 Database Design 2: Design Methodology Spring 2006.
JPA in Vaadin CSCI 3130 Winter What is JPA?  Java Persistence API  Allows for “easy” storage of Java Objects  Is a type of Object Relational.
1 ODB Design Handling Inheritance in ODL M. Akhtar Ali School of Informatics.
Chapter 2: Entity-Relationship Model. 3.2 Chapter 2: Entity-Relationship Model Design Process Modeling Constraints E-R Diagram Design Issues Weak Entity.
©Silberschatz, Korth and Sudarshan7.1Database System Concepts - 6 th Edition Chapter 7: Entity-Relationship Model.
David M. Kroenke and David J. Auer Database Processing Fundamentals, Design, and Implementation Chapter Five: Data Modeling with the Entity-Relationship.
Creating E/R Diagrams with SQL Server Management Studio, Writing SQL Queries D0ncho Minkov Telerik School Academy schoolacademy.telerik.com Technical Trainer.
Hibernate Annotation 李日貴 (jini) jakarta99 AT gmail.com SoftLeader Tech. Corp. Taiwan Java Annotation Lesson 1.
1 The Relational Data Model David J. Stucki. Relational Model Concepts 2 Fundamental concept: the relation  The Relational Model represents an entire.
XP Chapter 1 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Level 2 Objectives: Understanding and Creating Table.
Object-Relational Mapping. Contents  Default Mapping Rules  Elementary Mapping  Embeddables  Relationship Mapping  Inheritance Mapping.
Chengyu Sun California State University, Los Angeles
Hibernate (JPA) Code First Entity Relations
Presentation transcript:

Java Persistence API (JPA) Relationships

Kinds of relationships UML associations and aggregations (ER non- identifying relationships, shared semantics) One-to-one One-to-many Many-to-one Many-to-many UML compositions (ER identifying relationships, non- shared semantics) One-to-one One-to-many Java Persistence API - relationships2

@Entity public class Order implements Serializable private int id; private String private Shipment shipment;... public Shipment getShipment() { return shipment; } public void setShipment(Shipment shipment) { this.shipment = shipment; } Table Order has foreign key to table Shipment Java Persistence API - relationships3 Association and aggregation: unidirectional one-to-one

Attribute cascade Example above could be written this way private Shipment shipment; We specified that method persist() invoked on entity Order must cascade to related entity Shipment Attribute cascade may have these values: PERSIST, MERGE, REMOVE, REFRESH, and ALL Java Persistence API - relationships4

Association and aggregation: bidirectional public class Shipment implements Serializable private int id; private String city; private String private Order order; public Order getOrder() { return order; } public void setOrder(Order order) { this.order = order; } } Java Persistence API - relationships5 Table Order has foreign key to table Shipment because Order is owner of the relationship Table Order has foreign key to table Shipment because Order is owner of the relationship

Association and aggregation: unidirectional public class Employee implements Serializable private Company company; public Company getCompany() { return this.company; } public void setCompany(Company company){ this.company = company; } Java Persistence API - relationships6 Table Employee contains a foreign key to table Company.

Association and aggregation: bidirectional public class Company implements Serializable private Collection employees; public Collection getEmployees() { return this.employees; } public void setEmployees(Collection employees){ this.employees = employees; } Java Persistence API - relationships7

Association and aggregation: unidirectional one-to-many Per JPA specification: Unidirectional multi-valued relationships always have join table (!) That is, we cannot have unidirectional one-to-many relationship with the owner being on “one” side We can: create bidirectional relationship, owner must be “many” side or create join table Java Persistence API - relationships8

Association and aggregation: unidirectional public class Student implements private Collection courses; public Collection getCourses() { return courses; } public void setCourses(Collection courses) { this.courses = courses; } Java Persistence API - relationships9

Association and aggregation: bidirectional public class Course implements Serializable private Collection students; public Collection getStudents() { return students; } public void setStudents(Collection students) { this.students = students; } Java Persistence API - relationships10

Association and aggregation: what needs to be known For one-to-one bidirectional relationships, the owning side corresponds to the side that contains the corresponding foreign key The many side of one-to-many / many-to-one bidirectional relationships must be the owning side hence the mappedBy element cannot be specified on the ManyToOne annotation. For many-to-many bidirectional relationships either side may be the owning side Java Persistence API - relationships11

Association and aggregation: what needs to be known Bidirectional relationships will be persisted based on references held by the owning side of the relationship It is the developer’s responsibility to keep the in-memory references held on the owning side and those held on the inverse side consistent with each other when they change It is particularly important to ensure that changes to the inverse side of a relationship result in appropriate changes on the owning side, so as to ensure the changes are not lost when they are synchronized to the database Java Persistence API - relationships12

Kinds of relationships – aggregation Room – Chair identity of chair does not depend on identity of room chair is sharable – we can move chair from one room to another, and chair will not change its identity chair may exist without room – it may temporarily be placed outside of the house All these properties characterize aggregation relationship – relationship with sharable semantics Also known as non-identifying relationships in ER modeling Java Persistence API - relationships13

Kinds of relationships – composition House – Room identity of room depends on identity of house – room is always part of some house if house would change its identity (imagine reconstruction), rooms most probably would be renumbered room is non-sharable – we cannot move room from one house to another room cannot exist without house – if we destroy house, all its rooms are destroyed implicitly All these properties characterize composition relationship – relationship with non-sharable semantics Also known as identifying relationships in ER modeling Java Persistence API - relationships14

Implementing composition with JPA There are two ways to model composition (ER identifying) relationship: Usual one-to-many relationship with additionally applied non-shared semantics (automatic orphan deletion) – attribute orphanRemoval as noted above, programmer must take care of both ends of relationship Specialized relationship – embedded element collection ) it is enough to take care of one end of relationship Java Persistence API - relationships15

Composition: one-to-many with orphan removal may be specified with public class House orphanRemoval=true) Collection rooms; } This way we are requesting non-sharable semantics – if relation “child – parent” gets broken (parent is destroyed, or child is removed from parent’s collection), child is destroyed automatically Java Persistence API - relationships16

Composition: one-to-many with orphan removal In the example above: If room gets removed from house’s collection (e.g.: house.getRooms.remove(0) ), then the room will be deleted from DB automatically If house is deleted from database then all its rooms are deleted from database automatically Note: programmer must obey the rules of non- sharable semantics Removing one room from some house’s collection and adding it to other house’s collection would violate rules of non-sharable semantics Java Persistence API - relationships17

Java Persistence API - relationships18 Example

Composition: StudyJournal – Record embedded element collection public class StudyJournal implements name=“Record", private List records;... } Java Persistence API - relationships19

Composition: StudyJournal – Record embedded element // ! public class Record implements Serializable = "semester") private String = "courseTitle") private String = "assessment") private Integer assessment;... setters/getters... public boolean equals(Object obj) {...} public int hashCode() {...} } Java Persistence API - relationships20

Composition: Student – StudyJournal One-to-one relationship cannot be implemented as embedded element collection, we use orphanRemoval attribute instead: In entity = "student", orphanRemoval=true) private StudyJournal studyJournal ; In dependent @JoinColumn(name = "Student_id", referencedColumnName = "id") private Student student; Java Persistence API - relationships21

What is important for an architect Data/information models can be design in different abstraction levels: Logical UML class diagrams many kinds of relationships (association, aggregation, composition, etc.) normal forms are not being applied usually Logical ER models two relationships (identifying and not-identifying) normal forms are being applied Physical ER models data types and other physical properties (table partitioning etc.) are specified Java Persistence API - relationships22

What is important for an architect If database is designed with wrong relationships (aggregation in place of composition): you will not be able to use embeddable element collections you’ll have to relationship with orphan removal set to true and take care of both ends of the relationship – more chances that programmer will make a mistake Conclusion: architect must participate in data/information logical model reviews Java Persistence API - relationships23