I'm trying to create a simple INSERT and DELETE test of an entity using JPA:
User user = new User();
userEao = (GenericEAO<User, Long>) cxt.lookup("UserEAO/remote");
User user2 = userEao.persist(user);
userEao.flush();
The question is: when user is created should be in TRANSISTENT state, and when i persist it with
User user2 = userEao.persist(user);
i suppose that the user2 is a reference of a user in a MANAGED state.
Actually if i try to delete user with
userEao.delete(user);
jboss tell to me:
[DefaultDeleteEventListener] handling transient entity in delete processing
and it's correct, but if i try to delete the user2 jboss tell to me
javax.ejb.EJBException: java.lang.IllegalArgumentException: Removing a detached instance
and i dont' understand why the entity user2 is detached!
so i have BEFORE to merge it ( DETACHED -> MANAGED ) , and than to DELETE, and it works.
@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public User persist(User entity) {
getEntityManager().persist(entity);
return entity;
}
is it the problem? when the persist finish, it close the transaction and entity became detached?
i dont want to merge it, cause it causes another select that i want to avoid.
Or i should i cache the entity instead?
probably i didn't understand the JPA transactions, what you suggest me to read?
Thanks, Muzero