24
What’s new in JPA 2.0 Alin Burlacu Java Developer @ Endava 25.11.2011

Codecamp iasi-26 nov 2011-what's new in jpa 2.0

Embed Size (px)

Citation preview

Page 1: Codecamp iasi-26 nov 2011-what's new in jpa 2.0

What’s new in JPA 2.0

Alin Burlacu

Java Developer @ Endava

25.11.2011

Page 2: Codecamp iasi-26 nov 2011-what's new in jpa 2.0

Airplane view

Why ?

Serializable

JDBC

Platform independent

More OOP like

How ?

ORM (Object-Relational Mapping)

Entity manager

JPQL (Java Persistence Query Language)

Page 3: Codecamp iasi-26 nov 2011-what's new in jpa 2.0

What’s new

Expanded mapping functionality

Pessimistic locking

Expanded QL

Criteria API

Cache (L2) like a standard (minimal)

Page 4: Codecamp iasi-26 nov 2011-what's new in jpa 2.0

Expanded mapping functionality

@Access(Type Option)

Mix access modes in a hierarchy

Combine access modes in a single class

Page 5: Codecamp iasi-26 nov 2011-what's new in jpa 2.0

Expanded mapping functionality

Access Type Option - CODE

Page 6: Codecamp iasi-26 nov 2011-what's new in jpa 2.0

@Transient to prevent the field from being

mapped in addition to the property.

Page 7: Codecamp iasi-26 nov 2011-what's new in jpa 2.0

Expanded mapping functionality

Orphan Removal

Only relationships with single cardinality on

the source side :

@OneToOne

@OneToMany

Page 8: Codecamp iasi-26 nov 2011-what's new in jpa 2.0

Expanded mapping functionality

Orphan Removal - CODE

Page 9: Codecamp iasi-26 nov 2011-what's new in jpa 2.0

Be aware of object lifecycle

Page 10: Codecamp iasi-26 nov 2011-what's new in jpa 2.0

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

Page 11: Codecamp iasi-26 nov 2011-what's new in jpa 2.0

Expanded mapping functionality

@ElementCollection – CODE

(Collection of Basic Types)

Page 12: Codecamp iasi-26 nov 2011-what's new in jpa 2.0

Expanded mapping functionality

@ElementCollection - CODE

(Map of Basic Types)

Page 13: Codecamp iasi-26 nov 2011-what's new in jpa 2.0

Expanded mapping functionality

Other

@OrderColumn

@Embeded

@Embedable

@EmbededId

@OneToMany (no join Table)

Hierarchy of embedables

Relations to embedables

Page 14: Codecamp iasi-26 nov 2011-what's new in jpa 2.0
Page 15: Codecamp iasi-26 nov 2011-what's new in jpa 2.0

Pessimistic locking - CODE

Page 16: Codecamp iasi-26 nov 2011-what's new in jpa 2.0

Pessimistic locking - CODE

Page 17: Codecamp iasi-26 nov 2011-what's new in jpa 2.0

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”

Page 18: Codecamp iasi-26 nov 2011-what's new in jpa 2.0

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

Page 19: Codecamp iasi-26 nov 2011-what's new in jpa 2.0

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();

}

Page 20: Codecamp iasi-26 nov 2011-what's new in jpa 2.0

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();

}

Page 21: Codecamp iasi-26 nov 2011-what's new in jpa 2.0

Cache (L2) like a standard

(minimal)

Page 22: Codecamp iasi-26 nov 2011-what's new in jpa 2.0
Page 23: Codecamp iasi-26 nov 2011-what's new in jpa 2.0

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

Page 24: Codecamp iasi-26 nov 2011-what's new in jpa 2.0

Thank you