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 MethodCode:
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 (?,?,?)