These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Simple JPA question
PostPosted: Mon Sep 21, 2009 10:48 am 
Newbie

Joined: Mon Sep 21, 2009 10:38 am
Posts: 1
I'm trying to create a simple INSERT and DELETE test of an entity using JPA:

Code:
User user = new User();

userEao = (GenericEAO<User, Long>) cxt.lookup("UserEAO/remote");

User user2 = userEao.persist(user);

userEao.flush();



when user is created should be in TRANSISTENT state, and when i persist it with

Code:

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

Code:
userEao.delete(user);    


jboss tell to me:

Code:

[DefaultDeleteEventListener] handling transient entity in delete processing
   


and it's correct, but if i try to delete the user2 jboss tell to me

Code:
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.

Code:
@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 should i cache the entity instead?

probably it is not the right way to use the JPA transactions, what you suggest me to read?

Thanks, Muzero


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.