JPA: Extending the persistence context vs. detaching entities

Viewed 3089

There appear to be two patterns to implement business transactions that span several http requests with JPA:

  1. entity-manager-per-request with detached entities
  2. extended persistence context

What are the respective advantages of these patterns? When should which be preferred?

So far, I came up with:

  • an extended persistence context guarantees that object identity is equivalent to database identity, simplifying the programming model and potentially disspelling the need to implement equals for entities
  • detached entities require less memory than an extended persistence context, as the persistence context also has to store the previous state of the entity for change detection
  • no longer referenced detached entities become eligible for garbage collection; persistent objects must first be detached explicitly

However, not having any practical experience with JPA I am sure I have missed something of importance, hence this question.

In case it matters: We intend to use JPA 2.0 backed by Hibernate 3.6.

Edit: Our view technology is JSF 2.0, in an EJB 3.1 container, with CDI and possibly Seam 3.

1 Answers
Related