-->
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: merge() generated a "insert" SQL for managed Entity, why?
PostPosted: Wed May 19, 2010 3:31 am 
Newbie

Joined: Sat Apr 12, 2008 12:59 pm
Posts: 10
hihi,

When I using EntityManager.merge() method to update a Entity (team.class) that contained a list of Child Entities (member.class).

Before call EntityManager.merge() method, I uses entityManager.contains() method to
ensure the Child Entities are managed. The method returns "true".

but when I call EntityManager.merge(team) to update the parent entity, hibernate generated the "insert" SQL to update child entity and display "unique constraint" exception.

(P.S. Although, I changes to uses @PersistenceContext annotation, the problem still exists)

DAO Method
Code:

   public void updateTeam(Team team){
            
      entityManager.joinTransaction();
      JPAUtil.beginTransaction();
      
      try{
         
         log4jLogger.info("======= members("+team.getMembers().size()+") =======");
         for (Member member:team.getMembers()){            
            log4jLogger.info("Name: "+Member.getName());            
            log4jLogger.info("isManaged: "+isContainsMember(member));            
         }
         
         log4jLogger.info("isActive: "+entityManager.getTransaction().isActive());
         entityManager.merge(team);
         entityManager.flush();
         
      }catch (Exception e){         
         log4jLogger.info(SSAException.stack2String(e));
         entityManager.getTransaction().rollback();         
      }
   }


   public boolean isContainsMember(Object member){
      entityManager.joinTransaction();
      JPAUtil.beginTransaction();
      
      try{
         return entityManager.contains(member);
      }
   }


Log:
Code:
======= members(2) =======
Name: Alan
isManaged: true
Name: Alan2
isManaged: true
isActive: true
Hibernate: insert into TEAM_MEMBER(NAME,AGE,UUID) values (?,?,?)


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.