Hi all.
I need to know how can I set Hibernate (...) to achieve the following issue:
I have a bidirectional (composition) one-to-many association (a.bs is a Set object and b.a is a A object). When I load "a" from DB and I update one of its "bs" I need Hiernate intercept A entity when saveOrUpdate A.
Code:
public class A {
Set<B> bs = new HashSet<B>();
// ... other fields and setters/getters
}
public class B {
A a = null;
// ... other fields and setters/getters
}
Use case:
A a = load("idA"); // load A from DB
B b = s.getBById("idB"); // get a B element of A
b.setName("blablabla"); // update a field of B
saveOrUpdate(a); // persist A entity with its Bs (including modified B)
The change is performed because the (mini)model has been annotated properly.
The problem is that my interceptor only detects the change in B entity, but not A. I need to detect A change because I need to update audit info.
Other point of view is: I need to get A entity via B and update it. In fact, I can get A from B, but the change is not persisted...
Simplifying the question: I have to modify A entity (set a date) when my interceptor intercepts B entity. It worksfine in onSave but not in onFlushDirty. Why?
This is: When B is updated, is intercepted (onFlushDirty). The body of onFlushDirty method, among other things, do this:
b.getA().setLastModifyDate(new Date());
So, in that moment, A entity , that is attached to session, should became dirty, hence it should raise an interception action... I mean, the onFlushDirty method should be called again, his time for A entity. Am I wrong? But, in any case, A.lastModifyDate should be updated... and this is not happening!!!
Following I show the actual behaviour of my application:
- I create an A object
- I create a B object and I associate it to A
I persist A => A.lastModifyDate is the correct date (OK)
I create an A object
- I create a B object and I associate it to A
- I persist A => A.lastModifyDate is the correct date (OK)
I load the B object, I update it and I persist B -> A.lastModifyDate is the correct date (OK)
I create an A object
- I create a B object and I associate it to A
- I persist A => A.lastModifyDate is the correct date (OK)
I load A object, I update its B object and I persist A -> A.lastModifyDate is not the correct date (KO)
I create an A object
- I create a B object and I associate it to A
- I persist A => A.lastModifyDate is the correct date (OK)
I load A object, I update any A's field and also its B object and I persist A -> A.lastModifyDate is not the correct date (KO)
I create an A object and I persist it.
I load A object, I associate to it a new B object and I persist A => A.lastModifyDate is the correct date (OK)
I create an A object and I persist it.
- I load A object, I update any A's field, I associate to it a new B object and I persist A => A.lastModifyDate is the correct date (OK)
Any idea?
Thanks!