0
votes

I have a problem with my entity manager in my application. I have two DAO clasess like this:

@Repository
public abstract class DaoA 
{
    protected ClassA persistentClass;

    @PersistenceContext(name="my.persistence", type=PersistenceContextType.EXTENDED)
    protected EntityManager entityManager;
    -------------- some typical action for DAO --------------
}

Second DAO is for ClassB and looks similar to DaoA. The rest of things are done for me by the Spring framework.

When I'm debugging the application I recognize that both DAO objects have different instances of EntityManager. In the result my two different DAOs are connected with different PersistenceContext.

Question is if this is correct behaviour or not? I would like to have the same PersistenceContext for all my DAO classes. Please give me a hint if this is possible and if I understood the JPA correctly?

Regards Hsd

1

1 Answers

1
votes

It's a correct behaviour of EXTENDED persistence context, therefore you don't need it to be EXTENDED in this case.

In most cases you need a TRANSACTIONAL persistence context, that is the default mode when type is omitted:

@PersistenceContext(name="my.persistence")
protected EntityManager entityManager;

In this mode persistence context is associated with the transaction, so that all DAOs will share the same persistence context when working inside the same transaction. I guess it's what you need.