Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate/JPA Mapping on abstract class
    primarykey
    data
    text
    <p>What would be the best practice to implement the following situation with Hibernate.</p> <p>We define an abstract class that will form the base of any object that we want to persist in our database. It contains the id etc etc...</p> <pre><code>public abstract class ModelObject { protected int id; ... } </code></pre> <p>Now we subclass our base clase for special cases where more than one entity will have a similar fields.</p> <pre><code>public abstract class ModelObjectWithNose extends ModelObject { ... protected Nose nose; } </code></pre> <p>Now, for all classes that we want to have a <code>Nose</code>:</p> <pre><code>public class Person extends ModelObjectWithNose { ... } public class Animal extends ModelObjectWithNose { ... } </code></pre> <p>The real problem we have at the moment now is that this relationship needs to be bi-directional. Each concrete class needs to know which <code>Nose</code> is theirs, but each <code>Nose</code> also needs to know to what object it belongs.</p> <pre><code>public class Nose { ... private ModelObjectWithNose owner; } </code></pre> <p>For our example we need a <code>@OneToOne</code> relationship, as each <code>Person</code> can only have one <code>Nose</code> and each <code>Nose</code> can only belong to one <code>Person</code>.</p> <p>What we have tried is doing the following:</p> <pre><code>@Entity @Inheritance(strategy = InheritanceType.JOINED) public abstract class ModelObjectWithNose extend ModelObject { ... } </code></pre> <p>and then:</p> <pre><code>@Entity public class Person extends ModelObjectWithNose { ... } </code></pre> <p>...etc etc etc.</p> <p>1) How do I define the relationship between the <code>Nose</code> and the <code>ModelObjectWithNose</code>? Is it as simple as:</p> <pre><code>@Entity public class Nose { ... // this causes: 'Basic' attribute should not be 'Persistence Entity' ModelObjectWithNose owner; } </code></pre> <p>2) Is there a better / more preferred way? Would the inheritance strategy work properly?</p> <p>Thanks! </p> <p><em><strong>EDIT</em></strong></p> <p>So I have tried a few ways which all seem to potentially work. Using any of the three Inheritance schemes works fine if you set up the annotations as in the question. Unfortunately this has a huge impact on our existing database and code, so we opted to (for now) simply index the <code>Nose</code> object's <code>id</code> field that is kept on our current objects that would have extended <code>ModelObjectWithNose</code> and simply write helper methods to do point queries to find the entries.</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