Reputation: 61
I am trying to implement this JPA model:
Owner
is an Entity
. It has @Embedded
instance of class AbstractParent
.AbstractParent
is @Embeddable
. It is an abstract class, and this class
has two children.The children are concrete classes.The exception is: Cannot instantiate abstract class or interface: AbstractParent
It seems that (1) I need component inheritance, but (2) component inheritance is not allowed in JPA and (3) it was not implemented in Hibernate. Could you confirm that (1), (2) and (3) are correct? If they are correct could you provide any advice or workaround? I use hibernate-jpa 2.0, hibernate-core 3.5.1, hibernate-core-annotations 3.2.0
Upvotes: 5
Views: 4931
Reputation: 61
To answer questions why one may need this implementation: here is an example. Database table has information about computers, computers have monitors, if monitor is LCD then it's parameter is "pixels". If monitor is a TV then it's parameter is "lines". All monitors also have parameter "weight" Data structure of this table includes: id, RAM, monitor_indicator, weight, lines, pixels.
This data structure can be implemented in classes:
Computer
{id, RAM, display}
AbstractDisplay
{weight}
LCDDisplay
{pixels} inherits class AbstractDisplay
TVDisplay
{lines} inherits class AbstractDisplay
The basic idea is that main class (Owner/Computer) embeds another class (Parent/AbstractDisplay) that can have different set of parameters depending on type of the embedded class.
Regarding how embedded abstract class can be instantiated: same way as an abstract class is instantiated now in JPA: in the above example "monitor_indicator" indicates children class that should be used, this field must exist during the time when class Computer is instantiated.
Upvotes: 1
Reputation:
Embedding an Abstract
class doesn't make any logical sense, it can't be instantiated if it is Abstract
. You need to re-work your logic on why you think you need to embed an Abstract
class and not a specific implementation.
Upvotes: -2