Jpa + ejb how to keep entity attached (managed) to multiple requests?

Here is what I have tried so far.

I am making 2 requests for my singleton. The first request is processed by the firstRequest method and it updates the database. The second request is processed by the secondRequest method, but it does not change the database.

@Singleton
public class App{

@Inject DaoHuman daoHuman;
Human human;    

public void firstRequest(){
    human= daoHuman.findById(1);
    human.setAge(3);
}

public void secondRequest(){
    human.setAge(99);
}

      


@Stateful
public class DaoHuman{
    @PersistenceContext(unitName = "myPU", type=PersistenceContextType.EXTENDED)
    private EntityManager em;

      

My problem is that the entity becomes detached (unmanaged) after the first request. Therefore, it never receives a second request.

+3


source to share


1 answer


You have an extended persistence context defined for the Bean state session and indeed after daoHuman.findById(1)

(the code for this is not displayed, but I assume it is being invoked em.find(Humand.class, id))

, the person with Id = 1 will become a managed object and tracked by the extended persistence context.

In addition, since you have an extended persistence context, this context will last for the life of the session with the Bean condition, and the human object will remain manageable during multiple client calls (conversation).

It's all good. However, your Singleton is stateless, so when you make the second request, the person is not initialized.



You should also remember that your extended persistence context will only be associated with the current transaction when you invoke a session with a Bean condition where the EntityManager is defined. This does not happen with secondRequest()

, so when the transaction completes, the persistence context is not cleared / saved to the DB.

An easy way to use managed instance for multiple calls would be to store the object in the Sessionful Session Bean instance when the Bean is initialized first. Ive kept the singleton / Stateful session Bean structure in the code below. Depending on the requirements, I would be tempted to inject entity updates into the bean session.

@Singleton
public class App{

@Inject DaoHuman daoHuman;
Human human;    

    Public void init(int humanId){
        daoHuman.init(humanId);
}

    public void firstRequest(){
// Making the call to daoHuman.getHuman() both returns the mnaged entity and
// causes the persistence context to be associated with the current  
// transation, so when this method call ends, the context will be 
// Depending on requirementsflushed/committed.
        human= daoHuman.getHuman();
        human.setAge(3);
}

    public void secondRequest(){
        human= daoHuman.getHuman();
        human.setAge(99);
}

@Stateful
public class DaoHuman{
@PersistenceContext(unitName = "myPU", type=PersistenceContextType.EXTENDED)
private EntityManager em;
Human human;

    public void Init(int humanId){  
// em.find() will ensure the found entity becomes managed - part of the
// persistene context, placing it on the instance variable ensures    
// the managed entity is available over multple client invocatons (converstion)
        human = em.find(Human.class, humanId)
}
    public Human getHuman(){
        return human;
}

      

0


source







All Articles