5

I am trying to implement this JPA model:

  • Class Owner is an Entity. It has @Embedded instance of class AbstractParent.
  • Class 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

Kosta
  • 81
  • 1
  • 4
  • You can not embed Abstract classes.I you think about it you will see that it is logically incorrect as well . At the time you entity that has the abstract embedded class is created how would it know which implementation of this Abstract class to use. Issue in not with framework you need to work on your logic. Some one else might have a concrete answer but underlying problem is this what I said. – Shahzeb Aug 07 '11 at 23:56
  • 1
    Before tackling the question you've asked, lets take a step back. Why do you need/want an abstract parent class that is Embeddable? – Jeremy Aug 08 '11 at 00:10
  • Here's an example: I have a `GameCharacter`, which has a `List inventory`. `Item` is an abstract class, and `Potion` and `Ether` extend that abstract class. Every `Item` has a number of common functions, such as `drop` and `sell` that are the same for all items. Now I want to store that in a database using hibernate, but get this error. What do I do? – Kira Resari Jun 18 '20 at 11:57

2 Answers2

3

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:

  • Class Computer {id, RAM, display}
    • It has information about computers.
  • Abstract class AbstractDisplay {weight}
    • It has generic information about displays.
  • Class LCDDisplay {pixels} inherits class AbstractDisplay
    • it has LCD specific information (pixels).
  • Class TVDisplay {lines} inherits class AbstractDisplay
    • it has TV specific information (lines).

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.

calvin
  • 870
  • 1
  • 13
  • 22
Kosta
  • 81
  • 1
  • 4
-1

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.

  • 1
    Right, you could always model this as a many-to-one relationship to another entity hierarchy. I would argue, though, that embedding an abstract base class could make sense: Think about customers with different types of postal addresses (foreign versus domestic for example). You would then use an abstract base class and embed it in your costumer class. Hibernate does not support this, but it should theoretically be possible (by using a discriminator for example). – Stefan Haberl Oct 13 '12 at 09:41
  • Why can it not be instantiated? All it takes is any information about specific implementation, like name of a class that implements. There's a lot of abstract logic in Spring itself, still it manages to somehow instantiate abstract beans. Ask yourself if you had to implement such a logic for million dollars, would you be able to overcome the problem? If yes, then your posted answer is probably incorrect – Yuriy Kirel Nov 05 '22 at 18:49