1 The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
1
The following is intended to outline our general product direction. It is intended for information
purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should
not be relied upon in making purchasing decisions.
The development, release, and timing of any features or functionality described for Oracle’s
products remains at the sole discretion of Oracle.
• Java Persistence 1.0– Standardized object/relational mapping for Java applications– Available as part Java EE 5 Platform or standalone– Covered most of the essential features
• Java Persistence 2.0– More and better increased application portability – Released in December 2009– Available as part of Java EE 6 Platform or standalone– Reference Implementation is EclipseLink– Available as part of GlassFish v3
4
JavaTM Persistence 2.0: New Features
• Expanded modeling capabilities• Additional O/R mapping options• Additions to Java Persistence query language• Criteria API• Metamodel API• Pessimistic locking• Support for Bean Validation
5
JavaTM Persistence 2.0: Expanded modeling and mapping
• Collections of basic types• Collections of embeddables• Richer support for embeddable classes
– Multiple levels of embedding– Embeddable classes with relationships
• Persistently ordered lists• Improved map support
– Joins with additional columns– Ternary relationships
• Orphan deletion
6
Collections of Basic Types and Embeddables
• Collections of strings, integers, etc.• Collections of embeddables (e.g., Address, Detail)• Specified by @ElementCollection• Stored in “collection table”• Customize mappings with:
@Entitypublic class Person { @Id protected String ssn; protected String name; protected Date birthDate; ... @ElementCollection protected Set<String> nickNames; ...}
8
Collections of Basic Types
PERSONSSN NAME BIRTHDATE
PERSON_SSN NICKNAMES PERSON_NICKNAMES
9
Collections of Basic Types
@Entitypublic class Person { @Id protected String ssn; protected String name; protected Date birthDate; ... @ElementCollection @CollectionTable(name=“ALIASES”) @Column(name=“ALIAS”) protected Set<String> nickNames; ...}
10
Collections of Basic Types
PERSONSSN NAME BIRTHDATE
ALIASES PERSON_SSN ALIAS
11
Collections of Embeddable Types
@Entity public class Landlord { @Id String taxId; String name; @ElementCollection @CollectionTable(name=“rentals”) Set<Address> properties; ...}
@Embeddable public class Address { String street; String city; String state; ... }
12
LANDLORD_TAXID STREET CITY STATE …
Collections of Embeddable Types
LANDLORD TAXID NAME …
RENTALS
13
Persistently Ordered Lists
• Order is maintained in database by provider– Uses additional (integral) ordering column
• Specified with @OrderColumn• Provides alternative to @OrderBy
14
Persistently Ordered Lists
@Entity public class CreditCard { @Id String cardNumber; @ManyToOne Customer customer; ... @OneToMany(mappedBy=“creditCard”) @OrderColumn(name=“TXORDER”) List<CardTransaction> transactionHistory; ...}
@Entity public class CardTransaction { @Id @GeneratedValue Long id; @ManyToOne @JoinColumn(name=“CARDID”) CreditCard creditCard; @Temporal(DATE) Date txDate; ...}
15
CARDID ID TXDATE … TXORDERCARDTRANSACTION
OrderColumn
CREDITCARD CARDNUMBER …
16
Automatic Orphan Deletion
• Deletion of related entities when removed from relationship or when referencing entity is removed– For entities logically “owned” by “parent”– For one-to-one and one-to-many relationships
• Specified with orphanRemoval element
cascade=CascadeType.REMOVE is redundant
17
Orphan Deletion
@Entitypublic class Order { @Id int orderId; ...
@OneToMany(cascade=CascadeType.PERSIST,
orphanRemoval=true) Set<Item> items;}
18
Java Persistence Query Language:New Functionality
• Support for all new modeling and mapping features• Operators and functions in select list• Case, coalesce, nullif expressions• Restricted polymorphism• Collection-valued input parameters• Date / time / timestamp literals
19
New Operators
• INDEX– For ordered lists
• KEY, VALUE, ENTRY– For maps
• CASE, COALESCE, NULLIF– For case expressions and the like
• TYPE– For entity type expressions / restricted polymorphism
20
Ordered Lists, Date Literals
SELECT t FROM CreditCard c JOIN c.transactionHistory tWHERE c.customer.name = 'John Doe' AND INDEX(t) < 100 AND t.txDate <= {d '2010-6-30'}
SELECT eFROM Employee eWHERE TYPE(e) IN (PartTime, Contractor)
SELECT eFROM Employee e WHERE TYPE(e) IN :empTypes
22
Criteria API
• Object-based API for building queries• Designed to mirror JPQL semantics• Strongly typed
– Heavy use of Java generics– Based on type-safe metamodel of persistence unit– Typing carries through to query execution as well
• Supports object-based or string-based navigation
23
Criteria API: Core Interfaces
• CriteriaQuery– Represents a query definition object– Used to add / replace / browse constituent query elements– select, from, where, orderBy, groupBy, having,… methods
• CriteriaBuilder– Factory for CriteriaQuery objects– Obtained from EntityManager or EntityManagerFactory– Used to create selections, expressions, restrictions, orderings…
• Root– Query root
24
Criteria API: Core Interfaces
• Join, ListJoin, MapJoin,…– Joins from a root or existing join