codecamp iasi-26 nov 2011-what's new in jpa 2.0
TRANSCRIPT
What’s new in JPA 2.0
Alin Burlacu
Java Developer @ Endava
25.11.2011
Airplane view
Why ?
Serializable
JDBC
Platform independent
More OOP like
How ?
ORM (Object-Relational Mapping)
Entity manager
JPQL (Java Persistence Query Language)
What’s new
Expanded mapping functionality
Pessimistic locking
Expanded QL
Criteria API
Cache (L2) like a standard (minimal)
Expanded mapping functionality
@Access(Type Option)
Mix access modes in a hierarchy
Combine access modes in a single class
Expanded mapping functionality
Access Type Option - CODE
@Transient to prevent the field from being
mapped in addition to the property.
Expanded mapping functionality
Orphan Removal
Only relationships with single cardinality on
the source side :
@OneToOne
@OneToMany
Expanded mapping functionality
Orphan Removal - CODE
Be aware of object lifecycle
Expanded mapping functionality
@ElementCollection
Collections of simple data types mapped in
separate tables.
Map support has been extended so that
maps can have keys and values of basic
types, entities, or embeddables
Expanded mapping functionality
@ElementCollection – CODE
(Collection of Basic Types)
Expanded mapping functionality
@ElementCollection - CODE
(Map of Basic Types)
Expanded mapping functionality
Other
@OrderColumn
@Embeded
@Embedable
@EmbededId
@OneToMany (no join Table)
Hierarchy of embedables
Relations to embedables
Pessimistic locking - CODE
Pessimistic locking - CODE
Expanded QL Timestamp literals
SELECT t from BankTransaction t
WHERE t.txTime > {ts „2008-06-0110:00:01.0‟}
IN expression may include collection parameter
SELECT emp FROM Employee emp
WHERE emp.project.id IN [:projectIds]
Non- polymorphic queries
SELECT e FROM Employee e
WHERE CLASS(e) = FullTimeEmployee OR
e.wage = “SALARY”
Expanded QL
Ordered List indexing
SELECT t FROM CreditCard c
JOIN c.transactionHistory t
WHERE INDEX(t) BETWEEN 0 AND 9
CASE statement
UPDATE Employee e SET e.salary =
CASE e.rating WHEN 1 THEN e.salary * 1.1
WHEN 2 THEN e.salary * 1.05
ELSE e.salary * 1.01
END
Criteria API
STRING-BASED CRITERIA - CODE
{
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery c =
b.createQuery(Account.class);
Root acct = c.from(Account.class);
c.select(acct) .where(
cb.equal(acct.get(“name”), “Jim”));
return em.createQuery(c).getResultList();
}
Criteria API
STRONGLY TYPED CRITERIA - CODE
{
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Account> c =
cb.createQuery(Account.class);
Root<Account> acct = c.from(Account.class);
c.select(acct).where(
cb.equal(acct.get(Account_.name),
“Jim”));
Return em.createQuery(c).getResultList();
}
Cache (L2) like a standard
(minimal)
References JSR 317 (http://jcp.org/en/jsr/detail?id=317)
ProJPA 2 Mastering the Java™ Persistence API by Mike Keith and Merrick Schincariol
Beginning Java™ EE 6 Platform with GlassFish™ 3 by Antonio Goncalves
http://www.infoq.com/presentations/whats-new-and-exciting-in-jpa-20
http://en.wikibooks.org/wiki/Java_Persistence/What_is_new_in_JPA_2.0%3F
http://wiki.eclipse.org/EclipseLink/Examples/JPA#JPA_2.0
http://www.developer.com/features/article.php/3892261/JPA-20-Cache-Vs-Hibernate-Cache-Differences-in-Approach.htm
Thank you