-->
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.  [ 5 posts ] 
Author Message
 Post subject: ManyToMany relationship and callbacks
PostPosted: Thu May 17, 2007 1:50 pm 
Newbie

Joined: Thu May 17, 2007 1:07 pm
Posts: 2
Lets create the folowing cenário

Code:
@Entity
@EntityListeners(MyListener.class)
public class EntityA extends SuperEntity{

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   private Long id;

   private String description;

   @ManyToMany(cascade=CascadeType.ALL)
   private List<EntityB> entityBs  = new ArrayList<EntityB>();

... getters and setters

}

@Entity
@EntityListeners(MyListener.class)
public class EntityB extends SuperEntity{

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   private Long id;

   private String description;
.. getters and setters
}

public class MyListener {

   @PreUpdate
   public void someMethod1(Object a){
      System.out.println("@PreUpdate "+a);
   }
   @PrePersist
   public void someMethod2(Object a){
      System.out.println("@PrePersist "+a);
   }
   @PostPersist
   public void someMethod3(Object a){
      System.out.println("@PostPersist "+a);
   }
   @PostUpdate
   public void someMethod4(Object a){
      System.out.println("@PostUpdate "+a);
   }

}

public void testCode(){

em = emf.createEntityManager();
EntityA aNew = em.find(EntityA.class, 1L);
System.out.println(null != aNew.getEntityBs().remove(0)); // Actualy removes one entity
tx = em.getTransaction();
tx.begin();
em.persist(aNew);
tx.commit();
em.close();
}



The log for this operation with sql_show=true

Code:
Hibernate: select entitya0_.id as id0_0_, entitya0_.description as descript2_0_0_ from EntityA entitya0_ where entitya0_.id=?
Hibernate: select entitybs0_.EntityA_id as EntityA1_1_, entitybs0_.entityBs_id as entityBs2_1_, entityb1_.id as id1_0_, entityb1_.description as descript2_1_0_ from EntityA_EntityB entitybs0_ left outer join EntityB entityb1_ on entitybs0_.entityBs_id=entityb1_.id where entitybs0_.EntityA_id=?
true
Hibernate: delete from EntityA_EntityB where EntityA_id=?
Hibernate: insert into EntityA_EntityB (EntityA_id, entityBs_id) values (?, ?)


The listener works fine for almost every operation, except for the case of testeCode.

When the only change on the model is the relationship between to entitys the EntityManager persist completes sucessfull but does not call any callback.
I think that a similiar problem ocurs when using the hibernate API.

There is some way for me to know when Entity Manager tries to persist modification on the relationships between multiple entities?

I read the specs and I did not find anything specific about this matter, but i think that the Pre/PostPersist callback of EntityA should be called.

Regards

Name and version of the database you are using: Mysql or Hsql


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 17, 2007 8:56 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
in your code, you re persist an already managed object.
It should raise an exception

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 17, 2007 8:57 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
but appart from that, PostUpdate might be called, PreUpdate is more complex.

http://opensource.atlassian.com/projects/hibernate/browse/EJB-288

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 18, 2007 7:48 am 
Newbie

Joined: Thu May 17, 2007 1:07 pm
Posts: 2
Ok Emmanuel
thanks in advance

Just to be accurate with the other readers of this topic. The code works with or without the "em.persist()";

The code should not raise exception. From the specification:

Quote:
3.2.1 Persisting an Entity Instance
A new entity instance becomes both managed and persistent by invoking the persist method on it or
by cascading the persist operation.

The semantics of the persist operation, applied to an entity X are as follows:

• If X is a new entity, it becomes managed. The entity X will be entered into the database at or before transaction commit or as a result of the flush operation.

• If X is a preexisting managed entity, it is ignored by the persist operation. However, the persist operation is cascaded to entities referenced by X, if the relationships from X to these other entities is annotated with the cascade=PERSIST or cascade=ALL annotation element value or specified with the equivalent XML descriptor element.

• If X is a removed entity, it becomes managed.

• If X is a detached object, the EntityExistsException may be thrown when the persist operation is invoked, or the EntityExistsException or another PersistenceException may be thrown at flush or commit time.

• For all entities Y referenced by a relationship from X, if the relationship to Y has been annotated with the cascade element value cascade=PERSIST or cascade=ALL, the persist operation is applied to Y.



Top
 Profile  
 
 Post subject:
PostPosted: Fri May 18, 2007 10:40 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
arg you're right. I mixed persistent and detached.

_________________
Emmanuel


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

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.