About Question enthuware.oce-jpad.v6.2.512 :

Moderator: admin

Post Reply
unvector
Posts: 18
Joined: Sun Jun 21, 2015 2:56 am
Contact:

Re: About Question enthuware.oce-jpad.v6.2.512 :

Post by unvector »

Paul, thank you for your answer and for clarifying things.

I think that code used in the question is incorrect.

I tried to develop example similar to the one in the question, so I created two stateless beans:

Code: Select all

@Stateless
public class MyStateless {

    @PersistenceContext
    private EntityManager em;

    @EJB
    private MyService myService;

    public void createAndRemoveAddress() {
        Address address = new Address();
        address.setCity("Warszawa");
        em.persist(address);
        em.flush();
        myService.removeAddress(address);
    }
}

Code: Select all


@Stateless
public class MyService {

    @PersistenceUnit
    private EntityManagerFactory emf;

    @TransactionAttribute(value = TransactionAttributeType.MANDATORY)
    public void removeAddress(Address address) {
        EntityManager entityManager = emf.createEntityManager();
        entityManager.remove(address);
    }
}
As you see the semantics of MyService.removeAddress are pretty the same as in the question.

When I called MyStateless.createAndRemoveAddress() in client code I got IllegalStateEception:
Caused by: java.lang.IllegalArgumentException: Removing a detached instance model.Address#0
despite that there is no call to detach() and transaction still proceed.

My conclusion (from this experiment and from our discussion) is that application-managed entity manager has completely different persistent context from the transaction-scoped persistent context created before in JTA transaction, so even if there is an entity managed by the container-managed persistent context, it is seen as detached from the perspective of application-managed entity manager. So I think you should reformulate the example to not to use the app-managed persistent context.

There is no case when removeStudent() will correctly remove managed Student entity.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.oce-jpad.v6.2.512 :

Post by admin »

What if another method in the same bean creates an EntityManager using emf.createEntityManager() and loads an entity, and then calls removeStudent method?
If you like our products and services, please help us by posting your review here.

unvector
Posts: 18
Joined: Sun Jun 21, 2015 2:56 am
Contact:

Re: About Question enthuware.oce-jpad.v6.2.512 :

Post by unvector »

I added the following method to MyStateless bean:

Code: Select all

@Stateless
public class MyStateless {
    ...
    public void createAndRemoveAddressByAppManagedEM() {
        EntityManager em = emf.createEntityManager();

        Address address = new Address();
        address.setCity("Warszawa");
        em.persist(address);
        em.flush();

        myService.removeAddress(address);
    }
}
I've got the same IllegalArgumentException after calling createAndRemoveAddressByAppManagedEM():
Caused by: java.lang.IllegalArgumentException: Removing a detached instance model.Address#0

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.oce-jpad.v6.2.512 :

Post by admin »

You are right. Also noticed the following statement in Section 7.7: "An extended persistence context obtained from the application-managed entity manager is a stand-alone persistence context—it is not propagated with the transaction."

Paul.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 22 guests