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