Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I found a bit dirty workaround for this problem.</p> <p>Assuming there are only two ways to create an entity object of type <code>T</code>:</p> <ul> <li>Obtaining one from a <code>javax.inject.Provider&lt;T&gt;</code></li> <li>Quering it from the entity manager (which will call <code>@PostLoad</code> annotated methods).</li> </ul> <p>Further assuming you have a infrastructural baseclass for all of your entities, you can just add an entity listener to this entity. In this example I use static injection - maybe there is a nicer way.</p> <pre><code>@MappedSuperclass public abstract class PersistentDomainObject&lt;K extends Serializable &amp; Comparable&lt;K&gt;&gt; implements Comparable&lt;PersistentDomainObject&lt;K&gt;&gt;, Serializable { private static transient Injector injector; @PostLoad private final void onAfterLoaded() { injector.injectMembers(this); } @EmbeddedId private K id; public K getId() { return id; } // ... compareTo(), equals(), hashCode(), maybe a @Version member ... } </code></pre> <p>In your module setup you just need to call <code>requestStaticInjection(PersistentDomainObject.class);</code></p> <p>Now you simply can create entity classes like</p> <pre><code>@Entity public class MyDomainEntity extends PersistentDomainObject&lt;SomeEmbeddableIdType&gt; implements HatLegacyId { @Inject private transient MyDomainService myDomainService; private String name; // ... common stuff } </code></pre> <p>Bad thing about it, you have to trust in that noone will create a <code>MyDomainEntity</code> on his own but will ask a <code>Provider&lt;MyDomainEntity&gt;</code>for it. This could be provided by hiding the constructor.</p> <p>Kind regards,</p> <p>avi</p>
 

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