ORM and JPA 2.0. Zdeněk Kouba, Petr Křemen

Similar documents
ORM and JPA 2.0. Zdeněk Kouba, Petr Křemen

ORM and JPA 2.0. Zdeněk Kouba, Petr Křemen

ORM and JPA 2.0. Zdeněk Kouba, Petr Křemen

Chapter 7. The Annotations Alternative

Java Persistence API (JPA)

What data persistence means? We manipulate data (represented as object state) that need to be stored

CON 2013., : 본자료를무단배포하지말아주세요.

JPA. Java persistence API

Hibernate OGM Architecture

EJB 3 Entity Relationships

Dali Java Persistence Tools. User Guide Release for Eclipse

EJB 3 Entity Relationships

Java Persistence API. Patrick Linskey EJB Team Lead BEA Systems Oracle

Copyright 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13

Oracle Exam 1z0-898 Java EE 6 Java Persistence API Developer Certified Expert Exam Version: 8.0 [ Total Questions: 33 ]

Dali Java Persistence Tools. User Guide Release 2.0 for Eclipse

Advanced Web Systems 9- Hibernate annotations, Spring integration, Aspect Oriented Programming. A. Venturini

Polyglot Persistence. EclipseLink JPA for NoSQL, Relational, and Beyond. Shaun Smith Gunnar Wagenknecht

Java EE Architecture, Part Three. Java EE architecture, part three 1(69)

JPA The New Enterprise Persistence Standard

CO Java EE 6: Develop Database Applications with JPA

Java EE Architecture, Part Three. Java EE architecture, part three 1(24)

1 Copyright 2011, Oracle and/or its affiliates. All rights reserved.

Java EE Architecture, Part Three. Java EE architecture, part three 1(57)

Introduction to JPA. Fabio Falcinelli

Get Back in Control of your SQL

Dali Java Persistence Tools

A COMPONENT BASED ONLINE ORDERING SYSTEM USING ENTERPRISE JAVABEANS 3.0

Object Relational Mapping. Kenneth M. Anderson University of Colorado, Boulder Lecture 29 CSCI 4448/ /06/11

Database Connection using JPA. Working with the Java Persistence API (JPA) consists of using the following interfaces:

Produced by. Design Patterns. MSc Computer Science. Eamonn de Leastar

GET BACK IN CONTROL OF YOUR SQL. Lukas Eder

SQL, the underestimated Big Data technology

foreword to the first edition preface xxi acknowledgments xxiii about this book xxv about the cover illustration

Lightweight J2EE Framework

Java Persistence API (JPA) Entities

Advance Database Management System

Java EE 6 Community Roundtable. Reza Rahman Independent Consultant Author, EJB 3 in Action

Selecting a Primary Key - 2

JPA Enhancement Guide (v5.1)

Object-Relational Mapping

Generation of EJB3 Artifacts in a Modeling Platform

Basics of programming 3. Java Enterprise Edition

TP 6 des architectures logicielles Séance 6 : Architecture n-tiers avec du JPA avec plusieurs entités. 1 Préparation de l environnement Eclipse

Object-Relational Mapping Tools let s talk to each other!

Java Persistence API

Hibernate in close action. INF5750/ Lecture 3 (Part III)

CSE 530A. Lab 3. Washington University Fall 2013

SQL Data Manipulation Language. Lecture 5. Introduction to SQL language. Last updated: December 10, 2014

JSimpleDB: Language Driven Persistence for Java

Introduction to Java Enterprise Edition For Database Application Developer

Mappings and Queries. with. Hibernate

Deployment. See Packaging and deployment processes

Mysql Create Table Example Primary Key Foreign

HAVING clause, 234, 254 having() method, 234

find() method, 178 forclass() method, 162 getcurrentsession(), 16 getexecutablecriteria() method, 162 get() method, 17, 177 getreference() method, 178

IST311 Chapter 8: C# Collections - Index-Sequential Search List & Dictionary PROGRAM

CSC 261/461 Database Systems Lecture 6. Fall 2017

N : 1 양방향관계, 저장, 검색, 삭제실습

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

Developing Java EE 5 Applications from Scratch

The Good, the Bad and the Ugly

Name: Answers Q

"Charting the Course... Mastering EJB 3.0 Applications. Course Summary

EJB 3.0 Puzzlers. Mike Keith, Oracle Corp. Colorado Software Summit: October 21 26, 2007

Database Logical Design

Topics. Levels of data models. About persistence and databases. Relational database model. Pearson Education 2005 Chapter 8 (Maciaszek - RASD 2/e) 6

drawobject Circle draw

Fast Track to EJB 3.0 and the JPA Using JBoss

EXAMINATIONS 2009 END-YEAR. COMP302 / SWEN304 Database Systems / Database System Engineering. Appendix

Java Back-End Web App Tutorial Part 5: Managing Bidirectional Associations

Normalization in DBMS

Chapter 8 Persistence and Database Design

Topics in Enterprise Information Management

Introduction to Databases

Database Logical Design

Insertions, Deletions, and Updates

CS 327E Lecture 2. Shirley Cohen. January 27, 2016

Data Manipulation Language (DML)

Majustic - Evaluation Test With Answers. Time: 1 hour 30 minutes

Pirate Sports Database Tables/Fields

JPA. Java Persistence API

Relational Database Systems Part 01. Karine Reis Ferreira

Relational Model. IT 5101 Introduction to Database Systems. J.G. Zheng Fall 2011

2000 Lines of Java? Or 50 Lines of SQL? The Choice is Yours

1Z Oracle Database 11g - SQL Fundamentals I Exam Summary Syllabus Questions

Pro JPA 2. Mastering the Java Persistence API. Apress* Mike Keith and Merrick Schnicariol

Table of Contents. I. Pre-Requisites A. Audience B. Pre-Requisites. II. Introduction A. The Problem B. Overview C. History

Chapter 8 Persistence and Database Design

High Performance Persistence with EclipseLink

Database Programming with SQL

Lecture 07. Spring 2018 Borough of Manhattan Community College

(Note: 1. Each Question Carries 5 marks. 2. Solve any two questions) SET-I

Object Persistence and Object-Relational Mapping. James Brucker

CS6312 DATABASE MANAGEMENT SYSTEMS LABORATORY L T P C

VPM s Joshi-Bedekar College of Arts and Commerce TYBCOM Practical Exam Practice Questions MYSQL

Holon Platform JPA Module - Reference manual. Version 5.2.1

Hibernate Overview. By Khader Shaik

Database Design. 9-2 Basic Mapping: The Transformation Process. Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Collections Questions

Transcription:

ORM and JPA 2.0 Zdeněk Kouba, Petr Křemen

Collection Mapping Collection-valued relationship (above) @OneToMany @ManyToMany Element collections @ElementCollection Collections of Embeddable (new in JPA 2.0) Collections of basic types (new in JPA 2.0) Specific types of Collections are supported Set List Map

Collection Mapping public class Employee { @Id private int id; private String name; private long salary; @ElementCollection(targetClass=VacationEntry.class); private Collection vacationbookings; @ElementCollection private Set<String> nicknames; @Embeddable public class VacationEntry { @Temporal(TemporalType.DATE) private Calendar startdate; @Column(name= DAYS ) private int daystaken;

Collection Mapping public class Employee { @Id private int id; private String name; private long salary; @ElementCollection(targetClass=VacationEntry.class); private Collection vacationbookings; @ElementCollection private Set<String> nicknames;

Collection Mapping public class Employee { @Id private int id; private String name; private long salary; @ElementCollection(targetClass=VacationEntry.class); @CollectionTable( name= VACATION, joincolumn=@joincolumns(name= EMP_ID ); @AttributeOverride(name= daystaken, column= DAYS_ABS )) private Collection vacationbookings; @ElementCollection @Column(name= NICKNAME ) private Set<String> nickname; @Embeddable public class VacationEntry { @Temporal(TemporalType.DATE) private Calendar startdate; @Column(name= DAYS ) private int daystaken;

Collection Mapping public class Employee { @Id private int id; private String name; private long salary; @ElementCollection(targetClass=VacationEntry.class); @CollectionTable( name= VACATION, joincolumn=@joincolumns(name= EMP_ID ); @AttributeOverride(name= daystaken, column= DAYS_ABS )) private Collection vacationbookings; @ElementCollection @Column(name= NICKNAME ) private Set<String> nickname; VACATION EMP_ID @Embeddable public class VacationEntry { @Temporal(TemporalType.DATE) private Calendar startdate; @Column(name= DAYS ) private int daystaken;

Collection Mapping Interfaces: Collection Set List Map may be used for mapping purposes. An instance of an appropriate implementation class (HashSet, OrderedList, etc.) will be used to implement the respective property initially (the entity will be unmanaged). As soon as such an Entity becomes managed (by calling em.persist(...)), we can expect to get an instance of the respective interface, not an instance of that particular implementation class when we get it back (em.find(..)) to the persistence context. The reason is that the JPA provider may replace the initial concrete instance with an alternate instance of the respective interface (Collection, Set, List, Map).

Collection Mapping ordered List Ordering by Entity or Element Attribute ordering according to the state that exists in each entity or element in the List Persistently ordered lists the ordering is persisted by means of an additional database column(s) typical example ordering = the order in which the entities were persisted

Collection Mapping ordered List (Ordering by Entity or Element Attribute) public class Department { @OneToMany(mappedBy= department ) @OrderBy( name ASC ) private List<Employee> employees;

Collection Mapping ordered List (Ordering by Entity or Element Attribute) public class Department { @OneToMany(mappedBy= department ) @OrderBy( info.name ASC ) private List<Employee2> employees;

Collection Mapping ordered List (Persistently ordered lists) public class PrintQueue { @Id private String name; @OneToMany(mappedBy= queue ) @OrderColumn(name= PRINT_ORDER ) private List<PrintJob> jobs;

Collection Mapping ordered List (Persistently ordered lists) public class PrintQueue { @Id private String name; @OneToMany(mappedBy= queue ) @OrderColumn(name= PRINT_ORDER ) private List<PrintJob> jobs; This annotation need not be necessarily on the owning side

Collection Mapping Maps Map is an object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value. Keys: Basic types (stored directly in the table being referred to) Target entity table Join table Collection table Embeddable types ( - - ) Entities (only foreign key is stored in the table) Values: Values are entities => Map must be mapped as a one-to-many or many-to-many relationship Values are basic types or embeddable types => Map is mapped as an element collection

public class Employee { @Id private int id; private String name; private long salary; Collection Mapping Maps (keying by basic type key is String) @ElementCollection @CollectionTable(name= EMP_PHONE ) @MapKeyColumn(name= PHONE_TYPE ) @Column(name= PHONE_NUM ) private Map<String, String> phonenumbers;

Collection Mapping Maps (keying by basic type key is an enumeration) public class Employee { @Id private int id; private String name; private long salary; @ElementCollection @CollectionTable(name= EMP_PHONE ) @MapKeyEnumerated(EnumType.String) @MapKeyColumn(name= PHONE_TYPE ) @Column(name= PHONE_NUM ) private Map<PhoneType, String> phonenumbers; Public enum PhoneType { Home, Mobile, Work

Collection Mapping Maps (keying by basic type 1:N relationship using a Map with String key) public class Department { @Id private int id; private String name; @OneToMany(mappedBy= department ) @MapKeyColumn(name= CUB_ID ) private Map<String, Employee> employeesbycubicle;

Collection Mapping Maps (keying by basic type N:M relationship using a Map with String key) public class Department { @Id private int id; private String name; @ManyToMany @JoinTable(name= DEPT_EMP, joincolumns=@joincolumn(name= DEPT_ID ), inversejoincolumns=@joincolumn(name= EMP_ID )) @MapKeyColumn(name= CUB_ID ) private Map<String, Employee> employeesbycubicle;

Collection Mapping Maps (keying by entity attribute) public class Department { //... @OneToMany(mappedBy= department ) @MapKey(name= id ) private Map<Integer, Employee> employees;

public class Employee @Id @Column(insertable=false) private int id; Read-only mappings @Column(insertable=false, updatable=false) private String name; @Column(insertable=false, updatable=false) private long salary; The constrains are checked on commit! Hence, the constrained properties can be Modified in memory. @ManyToOne @JoinColumn(name= DEPT_ID, insertable=false, updatable=false) private Department department; //...