Thursday, February 27, 2014

JPA2 new features

What features came with JPA2?

The JPA2 was delivered with Java EE6. 
JPA2.1 was shipped with EE7 and is currently the latest version that can be used. 
Features:
  1. properties have been standardized
  2. support for using cache solutions
  3. better and finer support of lockings 
  4. enhancement of JPQL
  5. support of validation API
1. In the first JPA version the properties in the xml - configuration have been proprietary, so for each JPA provider the used property have been different:
in hibernate the url to the datasource was named "hibernate.connection.url" in toplink it was named "toplink.jdbc.url". Now common properties have been abstracted to:
<property name=“javax.persistence.jdbc.driver" 
 value=“XXX”/>
 <property name=“javax.persistence.jdbc.url" 
 value="XXX"/>
 <property name=“javax.persistence.jdbc.user" 
 value="XXX"/>
 <property name=“javax.persistence.jdbc.password" 
 value=“XXX"/>


2) Cache support allows main operations like 
  • does an entity exists in the cache: boolean contains(Class clazz, Object entity)
  • remove entity from cache: evict(Class clazz, Object entity)
  • remove all entities from a type: evict(Class clazz)
  • clear the cache: evictAll()

3) Better support for locking modes
  • OPTIMISTIC
  • OPTIMISTIC_FORCE_INCREMENT
  • PESSIMISTIC
  • PESSIMISTIC_FORCE_INCREMENT
For retrieval API of the entity manager you can specify one of the above mentioned modes or lock it after obtaining the entity: 
      EntityClassX entity = em.find(EntityClassX.class, id, PESSIMISTIC); 
vs.
     em.lock(entity, PESSIMISTIC);

Surely it is possibly to read the entity without severe lock mode, apply business logic on it and the obtain the lock to the end of the business transaction: 
     em.refresh(entity, PESSIMISTIC);



4) Enhancements of JPQL
  • date and time support like {d '2014-02-27'} or {t'14:00:00'}
  • member support: FROM ORDER O WHERE 'RECURRING_INVOICES' MEMBER OF O.TYPES
  • Collections comparing to empty: FROM ORDER O WHERE O.ORDERITEMS IS EMPTY
  • index support (retrieving rows based on the existing index of the table): WHERE INDEX(t) BETWEEN x AND y
  • ...

5) Validation
The validation part used in JPA2 is based on the specification in JSR303 and has the reference implemenation: HibernateValidator. 
Important to mention is that the JPA2 does not explicitly define a bean validation support. 
So the JPA provider could have a bean validator support. With Hibernate as the JPA provider, the Hibernate Validator is used.




No comments:

Post a Comment