Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are two possibilities to reuse the callback code from <code>ClassA</code> in <code>ClassB</code>:</p> <p><strong>I.</strong> The best/most elegant way is to move the whole code to a new class, say <code>MyEntityListeners</code> and then to use the <a href="http://docs.oracle.com/javaee/6/api/javax/persistence/EntityListeners.html" rel="nofollow">@EntityListeners</a> annotation on your entity classes like</p> <pre><code>@EntityListeners(class=MyEntityListeners.class) public class ClassB extends ClassA { ..... } public class MyEntityListeners { @PrePersist public void onPrePersist(Object entity) { //logic with entity (check the class of the entity or you can use `ClassA` instead of `Object`) } } </code></pre> <p>Please note that the <code>EntityListeners</code> are <a href="http://docs.jboss.org/hibernate/entitymanager/3.5/reference/en/html/listeners.html#d0e3053" rel="nofollow">inherited</a> in the subclasses from superclasses, so you do not need to do anything in <code>ClassB</code> if the EntiyListeners are already defined in <code>ClassA</code> (but you can add additional EntityListeners in ClassB, that are not in ClassA). For excluding <strong>all</strong> EntityListeners from the hierarchy you can use <a href="http://docs.oracle.com/javaee/6/api/javax/persistence/ExcludeSuperclassListeners.html" rel="nofollow">@ExcludeSuperclassListeners</a></p> <p><strong>II.</strong> If you have less callback methods and a small hierarchy tree, than you could overwrite and re-annotate every callback from <code>ClassA</code> also in <code>ClassB</code> like</p> <pre><code>... public class ClassB extends ClassA { ...... @Override @PrePersist public void myCallback() { super.myCallback(); } ...... } </code></pre>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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