Reputation: 1007
I am using Spring Data JPA with Hibernate and I have the following classes
@Entity
@Table(name = "ORDER_SLIP")
public class OrderSlip {
@EmbeddedId
OrderNumber orderNumber;
@Embedded
OrderDetails orderDetails;
}
@Embeddable
public abstract class OrderDetail implements Serializable {
String commonOrderDetailField;
}
@Embeddable
public class BuyOrderDetail extends OrderDetail implements Serializable {
String field1;
String field2;
}
@Embeddable
public class SellOrderDetail extends OrderDetail implements Serializable {
String field3;
String field4;
}
When I run the program, the fields in the OrderDetail class are embedded in the ORDER_SLIP table. The fields of the two subclasses of OrderDetail (BuyOrderDetail and SellOrderDetail) are not as I logically expect them to be.
Is this possible at all with JPA / Hibernate?
Upvotes: 2
Views: 4371
Reputation: 1005
You can implement inheritance between @Embeddable
classes. You just have to annotate the parent class with @MappedSuperclass
too.
So, e.g.:
@Embeddable
@MappedSuperclass
public class Parent {
@Basic
private String parentProperty;
// ... getters/setters
}
@Embeddable
public class Child extends Parent {
@Basic
private String childProperty;
// ... getters/setters
}
This way Hibernate (tested with 5.x) will map both parentProperty
and childProperty
correctly in the Child
class.
In your example you can take advantage of the inheritance only if you use one of your subtypes in the OrderSlip class (not the parent type).
Upvotes: 1
Reputation: 1010
This is not possible. You should take a look at https://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html_single/#inheritance
Upvotes: 2