Note that there are some explanatory texts on larger screens.

plurals
  1. POJPA Inheritance
    primarykey
    data
    text
    <p>Hi I'm new to JPA and I'm having trouble understanding how it handles inheritance.</p> <p>I have a specific problem I need solved without changing the DB scheme, but if you can't find a solution I would appreciate solution suggestions with a different DB scheme (Hibernate/TopLink solutions welcome).</p> <p>If I was unclear or you need more information, please tell me so. Thanks in advance!</p> <p>I have this database:</p> <pre><code>TABLE Fruit Id Varchar (10) Primary Key size Varchar (10) fruit_type Varchar(10) TABLE Apple Id Varchar (10) Primary Key Foreign Key references Fruit.Id Apple_Property Varchar(10) </code></pre> <p>So far my entities look like this :</p> <pre><code>@Entity @Inheritance(strategy=InheritanceType.JOINED) @DiscriminatorColumn(name="fruit_type", discriminatorType=DiscriminatorType.Char) @DiscriminatorValue(value="fruit") public class Fruit implements Serializable { @Id protected String Id; protected String size; } @Entity @DiscriminatorValue(value="apple") //@PrimaryKeyJoinColumn(name="Id" , referencedColumnName="Id") public class Apple extends Fruit implements Serializable { private String Apple_Property; } </code></pre> <p>Currently I am able to persist Fruit objects without a problem.. Apple objects persist only when their Fruit object hasn't been persisted yet.</p> <p>If I try to persist an apple object with an already persisted Fruit object :</p> <pre><code>Fruit fruit1 = new Fruit("1", "Small"); Apple apple1 = new Apple(fruit1, "red"); provider.create(fruit1); provider.create(apple1); </code></pre> <p>I will get an error since JPA tries to create a new row on Fruit table with Id="1" which already exists.</p> <p>..</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.
    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