Gotchas - rafalkieras/jpa-kss GitHub Wiki

#Gotchas

##Property access type

Getters with side effects will result in update queries.

##Bulk operations

A bulk operation applies to entities of the specified class and its subclasses. It does not cascade to related entities.

delete from Account

Bulk updates bypass the optimistic locking check. Version needs to be checked/updated manually.

The persistence context is not synchronized with the result of the bulk update/delete. It may cause iconsistences between currently managed entities in the active persistence context and the database.

##N+1 Problem

Common performance antipattern, it occurs with lazy loaded collections.

##Merging

The merge operation signature: <T> T merge(T entity)

The entity passed into this method is still not associated with the persistence context (it is not tracked for changes). The merge operation returns the managed instance that the state was merged to. If an entity instance with the same identifier already exists in the persistence context, the provider will overwrite its state with the state of the entity that is being merged.

The version field (if defined) is used to ensure integrity when performing the merge operation.

It is not possible to merge removed entity - the merge operation will throw IllegalArgumentException.

##EntityTransaction

It is not possible to control JTA transactions via EntityTransaction interface (unlike Hibernate's Transaction interface). EntityManager#getTransaction

##Batch processing

JPA needs to be carefully used for batch processing. You may encounter memory issues (OOM exceptions) when processing a lot of objects in one session. This is because all objects will be cached and tracked by the EntityManager and its persistence context. Batch processing in Hibernate

##Refreshing entity state

The refresh operation of the EntityManager interface is the only way to make a managed entity up to date with the database.

##Transaction rollback

Transaction rollback causes all pre-existing managed instances and removed instances to become detached.