Hi I'm new to JPA and I'm having trouble understanding how it handles inheritance.
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).
If I was unclear or you need more information, please tell me so. Thanks in advance!
I have this database:
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)
So far my entities look like this :
@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;
}
Currently I am able to persist Fruit objects without a problem.. Apple objects persist only when their Fruit object hasn't been persisted yet.
If I try to persist an apple object with an already persisted Fruit object :
Fruit fruit1 = new Fruit("1", "Small");
Apple apple1 = new Apple(fruit1, "red");
provider.create(fruit1);
provider.create(apple1);
I will get an error since JPA tries to create a new row on Fruit table with Id="1" which already exists.
..