Java – JPA: extending persistence context and separating entities

There seem to be two modes to implement business transactions across multiple HTTP requests and jpas:

>Entity manager with separate entities > extended persistence context per request

What are the advantages of these models? When should it be the first choice?

So far, I have come up with:

>The extended persistence context ensures that the object ID is equal to the database ID, simplifies the programming model, and potentially destroys the identity of the entity. The entities that need to be separated need less memory than the extended persistence context, because the persistence context must also store the previous state of the entity used for change detection > no longer reference the separated entity, which is eligible for garbage collection; Persistent objects must first be explicitly separated

However, without JPA's practical experience, I believe I missed some important things, so this problem

If important: we intend to use JPA 2.0.0 supported by hibernate 3.6

Editor: our view technology is JSF 2.0. In an EJB 3.1 container, it has CDI and possibly seam 3.0

Solution

Then I can enumerate the challenges by trying to use the extended persistence context in the web environment Some things also depend on your point of view, what technology is, if it is a bound entity or view level intermediary

>The entity manager is not thread safe You do not need one per user session A browser tag is required for each user session. > When an exception occurs, the entitymanager is considered invalid and needs to be closed for replacement If you plan to write your own framework extension, in order to manage the extended life cycle, you need to implement bulletproof. Generally, set exceptions in EM per request, go to some error pages and then load the next page to create one. Anyway, it will always be like this. > The equality of objects will not be 100% automatic security As mentioned above, an exception may be invalid context. A loaded object is associated, so a fetch now will not do this equally. It also assumes a very high level of skills and understanding of the working principle of JPA and what EM developers use it For example, When merging is used unexpectedly, it is not necessary to return a new object that does not meet = = with the same domain (like treatment merging, SQL 'Update' is a very common JPA Noobie 'error', especially because it just doesn't work most of the time, so it slides past.) > If you use view technology combined with POJO (such as spring MVC), you intend to bind web form data and send it directly to your entity, you will soon encounter trouble Changes to additional entities will continue in the next flush / commit, whether or not they are completed in the transaction The common error is that the web form enters and binds some invalid data to an entity. If the verification fails, try returns to the screen to notify the user The building error screen involves running a query Query trigger refresh / commit persistence context The change is bound to the attachment entity and is refreshed to the database, hoping to cause SQL exceptions, but perhaps just adhere to corrupt data

(of course, if programming is in a hurry, problem 4 will also occur in the session, but you won't be forced to actively try to avoid it.)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>