Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate interceptor: entity intercepted when a collection element updated
    primarykey
    data
    text
    <p>I need to know how can I set Hibernate (...) to achieve the following issue:</p> <p>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.</p> <p>Code:</p> <pre><code>public class A { Set&lt;B&gt; bs = new HashSet&lt;B&gt;(); // ... other fields and setters/getters } public class B { A a = null; // ... other fields and setters/getters } </code></pre> <p>Use case:</p> <pre><code>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) </code></pre> <p>The change is performed because the (mini)model has been annotated properly.</p> <p>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.</p> <p>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...</p> <p>Simplifying the question: <strong>I have to modify A entity (set a date) when my interceptor intercepts B entity. It worksfine in onSave but not in onFlushDirty. Why?</strong></p> <p>This is: When B is updated, is intercepted (onFlushDirty). The body of onFlushDirty method, among other things, do this:</p> <pre><code>b.getA().setLastModifyDate(new Date()); </code></pre> <p>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!!!</p> <p>Following I show the actual behaviour of my application:</p> <ol> <li>I create an A object</li> <li>I create a B object and I associate it to A</li> <li><p>I persist A => A.lastModifyDate is the correct date (<strong>OK</strong>)</p></li> <li><p>I create an A object</p></li> <li>I create a B object and I associate it to A</li> <li>I persist A => A.lastModifyDate is the correct date (<strong>OK</strong>)</li> <li><p>I load the B object, I update it and I persist B -> A.lastModifyDate is the correct date (<strong>OK</strong>)</p></li> <li><p>I create an A object</p></li> <li>I create a B object and I associate it to A</li> <li>I persist A => A.lastModifyDate is the correct date (<strong>OK</strong>)</li> <li><p>I load A object, I update its B object and I persist A -> A.lastModifyDate <strong>is not</strong> the correct date (<strong>KO</strong>)</p></li> <li><p>I create an A object</p></li> <li>I create a B object and I associate it to A</li> <li>I persist A => A.lastModifyDate is the correct date (<strong>OK</strong>)</li> <li><p>I load A object, I update any A's field and also its B object and I persist A -> A.lastModifyDate <strong>is not</strong> the correct date (<strong>KO</strong>)</p></li> <li><p>I create an A object and I persist it.</p></li> <li><p>I load A object, I associate to it a new B object and I persist A => A.lastModifyDate is the correct date (<strong>OK</strong>)</p></li> <li><p>I create an A object and I persist it.</p></li> <li>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 (<strong>OK</strong>)</li> </ol> <p>Any idea?</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload