Chengyu Sun California State University, Los Angeles

Slides:



Advertisements
Similar presentations
V 6, Mats Strandberg ORM With Hibernate.
Advertisements

Introduction to NHibernate By Andrew Smith. The Basics Object Relation Mapper Maps POCOs to database tables Based on Java Hibernate. V stable Generates.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
The Java Persistence API Edel Sherratt. Contents Revisit applications programming Using Java Persistence API.
Data Persistence and Object-Relational Mapping Slides by James Brucker, used with his permission 1.
Java Persistence API Maciej Adamiak. Agenda -Entity, -Entity Operations, -Query Language.
Rice KRAD Data Layer JPA Design Eric Westfall July 2013.
CSE446 S OFTWARE Q UALITY M ANAGEMENT Spring 2014 Yazılım ve Uyguluma Geliştirme Yöneticisi Orhan Başar Evren.
Maven for building Java applications By Nalin De Zoysa
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan Lecture-02,03 Introduction –Data Models Lectured by, Jesmin Akhter.
JPA Java Persistence API. Introduction The Java Persistence API provides an object/relational mapping facility for managing relational data in Java applications.
Data Access Patterns Some of the problems with data access from OO programs: 1.Data source and OO program use different data modelling concepts 2.Decoupling.
NHibernate in Action Web Seminar at UMLChina By Pierre Henri Kuaté 2008/08/27
CHAPTER 14 USING RELATIONAL DATABASES TO PROVIDE OBJECT PERSISTENCE (ONLINE) © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database.
CHAPTER 8: MANAGING DATA RESOURCES. File Organization Terms Field: group of characters that represent something Record: group of related fields File:
Geodatabase Relationships & Relationship Classes GIS 458 Spring 2006 Larry Clamp.
Optimize Your Object Model Paul Dayan – Sales Engineer.
CS 157B: Database Management Systems II February 4 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak.
Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.
OK lets start, who am I ?. Nayden Gochev (a.k.a. JOKe) Java Spring Android Hybris GWT EJB JSF RMI JAX-RS Struts JMS JPA Hibernate C# ASP.NET TestStudio.
EJB 3.0 Persistence Based on: Patel, Brose, Silverman, Mastering Enterprise JavaBeans 3.0.
1 Mapping to Relational Databases Presented by Ramona Su.
Java Data Persistence Using Hibernate Jack Gardner October 2004.
An Introduction to Object/Relational Persistence and Hibernate Yi Li
Burapha University, 2003 Object-Oriented Analysis Basic of Object Mapping to Relational Database.
Middleware Technology (J2EE/EJB) Entity Bean (JBoss EJB 3.0 tutorial)
Topic : Hibernate 3:Advanced ORM Kaster Nurmukan.
Copyright © 2013 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill/Irwin APPENDIX C DESIGNING DATABASES APPENDIX C DESIGNING DATABASES.
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.
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.
©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Entity-Relationship Model Entity Sets Relationship Sets Mapping Constraints Keys.
Chapter 2: Entity-Relationship Model. 3.2 Chapter 2: Entity-Relationship Model Design Process Modeling Constraints E-R Diagram Design Issues Weak Entity.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 7: Entity-Relationship.
Data Modeling AND ER MODELS.
CS422 Principles of Database Systems Object-Relational Mapping (ORM) Chengyu Sun California State University, Los Angeles.
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.
CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.
CS422 Principles of Database Systems Convert ER Design to Relations Chengyu Sun California State University, Los Angeles.
CS422 Principles of Database Systems Entity-Relationship Model Chengyu Sun California State University, Los Angeles.
Hibernate Annotation 李日貴 (jini) jakarta99 AT gmail.com SoftLeader Tech. Corp. Taiwan Java Annotation Lesson 1.
CS422 Principles of Database Systems Object-Oriented Features in DBMS Chengyu Sun California State University, Los Angeles.
CS520 Web Programming Spring – Web MVC Chengyu Sun California State University, Los Angeles.
Object-Relational Mapping. Contents  Default Mapping Rules  Elementary Mapping  Embeddables  Relationship Mapping  Inheritance Mapping.
CS520 Web Programming Object-Relational Mapping with Hibernate and JPA Chengyu Sun California State University, Los Angeles.
CS422 Principles of Database Systems Entity-Relationship Model
Don't Know Jack About Object-Relational Mapping?
CS6320 – Java Persistence API
CS3220 Web and Internet Programming Database Access with JDBC
Chengyu Sun California State University, Los Angeles
CS3220 Web and Internet Programming More SQL
Java Persistence: Object Inheritance Mapping
CS320 Web and Internet Programming SQL and MySQL
Chengyu Sun California State University, Los Angeles
Advanced Java Programming
Hibernate (JPA) Code First Entity Relations
Database Applications (15-415) ORM - Part I Lecture 11, February 11, 2018 Mohammad Hammoud.
CS2011 Introduction to Programming I Objects and Classes
JPA: Mapping to an existing database
Chengyu Sun California State University, Los Angeles
CS3220 Web and Internet Programming SQL and MySQL
CS4961 Software Design Laboratory Understand Aquila Backend
Rules and Tips for Good Web Programming (and Programming in General)
Chengyu Sun California State University, Los Angeles
CS3220 Web and Internet Programming SQL and MySQL
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
Presentation transcript:

Chengyu Sun California State University, Los Angeles CS5220 Advanced Topics in Web Programming Object-Relational Mapping with Hibernate and JPA (II) Chengyu Sun California State University, Los Angeles

Schema Generation Generate DDL from annotated Java classes To file/console To database Standardized in JPA 2.1 SchemaExport in the Hibernate example More in Chapter 9.4 of JPA 2.1 Specification

Basic Object-Relational Mapping 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, @OneToMany, @ManyToMany, and @OneToOne

Advanced ORM Embedded class Collection Inheritance

Embedded Class public class Address { String street; String city; String state; String zip; } public class User { Integer id; String username String password; Address address; } users id … street city state zip

Mapping Embedded Class @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; }

Collection of Simple Types public class Customer { Integer id; String name; String address; Set<String> phones; }

Mapping Element Collection Set<String> phones; customers Customer_phones id Customer_id phones

Customize Collection Table @ElementCollection @CollectionTable( name = “customer_phones”, joinColumns=@JoinColumn(name = “customer_id”) ) @Column(name=“phone”) Set<String> phones;

From Set to List … Elements in a list are ordered Rows in a relational table are not ordered public class Customer { Integer id; String name; String address; List<String> phones; }

… From Set to List Use the natural order (e.g. alphabetic order) of an existing column Use an additional column to keep the order customer_id phone customer_id phone phone_order 1 (323) 343-6789 1 (323) 343-6789 2 1 (626) 353-4567 1 (626) 353-4567 1 1 (323) 343-1234 1 (323) 343-1234 2 (323) 343-1111 2 (323) 343-1111 2 (626) 353-2222 2 (626) 353-2222 1 Customer 1’s list of phones: Customer 1’s list of phones: (323) 343-1234 (323) 343-6789 (626) 353-4567 (323) 343-1234 (626) 353-4567 (323) 343-6789

List of Simple Types Order by property Order by a separate column @OrderBy(“<property_name> ASC|DESC”) Simple types do not have properties @ElementCollection @OrderBy(“asc”) List<String> phones; Order by a separate column @ElementCollection @OrderColumn(name = “phone_order”) List<String> phones;

Issues Related to Collections of Class Types Unidirectional vs. Bidirectional Association Set vs. List Lazy Loading and Cascading Behaviors

Association Example A customer may own multiple accounts An account only has one owner

Unidirectional Association #1 public class Account { Integer id; Double balance; Date createdOn; @ManyToOne Customer owner; } public class Customer { Integer id; String name; String address; Set<String> phones; }

Bidirectional Association public class Account { Integer id; Double balance; Date createdOn; @ManyToOne Customer owner; } public class Customer { Integer id; String name; String address; Set<String> phones; @OneToMany(mappedBy=“owner”) Set<Account> accounts; } property

Unidirectional Association #2 public class Account { Integer id; Double balance; Date createdOn; } public class Customer { Integer id; String name; String address; Set<String> phones; @OneToMany Set<Account> accounts; } Do not do this as it will be handled as a many-to-many association which is less efficient

Using List … public class Customer { Integer id; String name; String address; Set<String> phones; @OneToMany(mappedBy=“owner”) @OrderBy( “createdOn asc” ) List<Account> accounts; }

… Using List Avoid using list with @OrderColumn in a bidirectional association See Section 2.4.6.2.1 in Hibernate Annotation Reference Guide See AnswerSheet and AnswerSection in CSNS2

Many-To-Many Example A customer may own multiple accounts An account may have multiple owners

Mapping Many-To-Many public class Account { Integer id; Double balance; Date createdOn; @ManyToMany Set<Customer> owners; } public class Customer { Integer id; String name; String address; Set<String> phones; @ManyToMany(mappedBy=“owners”) Set<Account> accounts; } property

Customize Join Table @ManyToMany @JoinTable( name = “account_owners”, joinColumns=@JoinColumn(name = “account_id”), inverseJoinColumns=@JoinColumn(name=“owner_id”) ) Set<Customer> owners;

Lazy Loading entityManager = entityManagerFactory .createEntityManager(); Customer c = entityManager.find( Custoemr.class, 1 ); // c is loaded, but c’s accounts are not Integer accountId = c.getAccounts().get(0).getId(); // c’s accounts are loaded from database when // c.getAccounts() is called  Lazy Loading entityManager.close(); // Lazy loading can only happen inside the same entity manager // (a.k.a. persistent context)

Cascading Behavior Whether an operation on the parent object (e.g. Customer) should be applied to the children objects in a collection (e.g. List<Account>) 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??

Cascading Types in JPA http://sun.calstatela.edu/~cysun/documentation/jpa-2.0-api/javax/persistence/CascadeType.html

CascadeType Examples @OneToMany(mappedBy=“owner”, cascade=CascadeType.PERSIST) List<Account> accounts; @OneToMany(mappedBy=“owner”, cascade={CascadeType.PERSIST, CascadeType.MERGE}) List<Account> accounts; @OneToMany(mappedBy=“owner”, cascade=CascadeType.ALL) List<Account> accounts;

Inheritance public class CDAccount extends Account { Integer term; }

Everything in One Table accounts id account_type balance created_on term Discriminator column

Inheritance Type – SINGLE_TABLE @Entity @Table(name=“accounts”) @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name=“account_type”) @DiscriminatorValue(“CHECKING”) public class Account { … } @Entity @DiscrimnatorValue(“CD”) public class CDAccount extends Account { … }

Table Per Subclass accounts cd_accounts id balance created_on foreign key cd_accounts account_id term

Inheritance Type – JOINED @Entity @Table(name=“accounts”) @Inheritance(strategy=InheritanceType.JOINED) public class Account { … } @Entity @Table(name=“cd_accounts”) public class CDAccount { … }

Table Per Concrete Class accounts id balance created_on cd_accounts id balance created_on term

Inheritance Type – TABLE_PER_CLASS @Entity @Table(name=“accounts”) @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS) public class Account { … } @Entity @Table(name=“cd_accounts”) public class CDAccount { … }

Tips for O/R Mapping Understand OO design Understand relational design Make sure the application design is object-oriented Understand relational design Know what the database schema should looks like before doing the mapping

More HQL Examples (I) … class User { Integer id; String username; … } class Section { Integer id; User instructor; … } users sections id username id instructor_id 1 cysun 1 1 2 vcrespi 2 1 3 2

… More HQL Examples (I) Query: find all the sections taught by the user “cysun”. SQL?? HQL??

More HQL Examples (II) … class User { Integer id; String username; … } class Section { Integer id; Set<User> instructors; … } users sections instructors id username id section_id instructor_id 1 cysun 1 1 1 2 vcrespi 2 2 1 3 2 2

… More HQL Examples (II) Query: find all the sections for which “cysun” is one of the instructors SQL?? HQL?? See SectionDaoImpl in CSNS2 for more HQL join examples

Readings Hibernate ORM User Guide Chapter 2 Domain Model Chapter 15 HQL and JPQL Chapter 24 Mapping Annotations