Java EE – how can multiple requests (using wicket and JPA) attach entities (or their associations) to the current persistence context?

I am using wicket based web application on Java EE

I'm trying to find a way to ensure that any entity used as a model object is always attached to the current entitymanager, wicket will try to render any component In this way, when the component grabs data from its model, the data can be easily loaded by the entity as needed

There are many tutorials, and some posts here cite loadable detachable models (LDM) as a solution This is useful when we do not need to maintain any state between intermediate requests In this case, whenever the page is rendered, LDM will load the latest version of the required entity from the database

However, sometimes users need to edit the data statefully through multiple steps before saving the data, so the model needs to keep the entity in the "unsaved" state LDM will effectively eliminate user changes at each step

So far, we have been using a model that combines entities with persistence contexts This is a simplified version:

Our entitymanager is injected by GlassFish, which spans a complete servlet request, so when an entity is attached to a persistence context, it will remain attached until the page is rendered

The above model deals with cases where entities have been persisted and are being edited Whenever a component on the page calls GetObject () in this model, the model will merge the entity with the persistence context, and we can freely navigate the entire object graph of the entity without throwing any lazyinitializationexceptions

However, we cannot use this model when the entity is new and has not been persisted, because the entity is not ready to merge This usually happens when a user is creating a new entity and still needs to fill it in through a form In this case, we want to have the same degree of freedom to navigate the object graph of the entity (because some associations have been set, such as the parent of the entity), without worrying about lazyinitializationexception

A similar solution is described as here (option #3), but does not include the above "new entity" use case

Has anyone encountered such use cases? How did you solve it? Is there a better way to integrate wicket with JPA than through custom imodel?

Solution

In my case, I usually create a dto to keep all the data I need to create an entity

>Perform validation > create a new entity from the edited dto > inject references to other entities stored in these separate LDM models. > Stick to the entity

It's a bit troublesome, but it works

Conversations across multiple requests are not available in pure portals and may never - this is not the area of the ticket gate

Seam supports long conversations, and it supports small doors (I've never used seams - I can't advise you on this)

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
分享
二维码
< <上一篇
下一篇>>