V
V
Vitaly Stolyarov2016-12-13 18:10:43
Hibernate
Vitaly Stolyarov, 2016-12-13 18:10:43

How to inherit @Entity not mapped class in Hibernate?

The Field entity is required, which will be inherited by such classes as SliderField, DateField, etc., while they only perform the wrapper function and their data is already stored by Field in the form of an ArrayList
. It looks like this:

@Entity
class Field{

 public String val1;
// ...

public ArrayList<Data> data;

//...
}

class SliderField extends Field{
 public int min.max;

public void setMin(int m);
// ....
}

And in this case, Hibernate swears "Unknown entity: com.pkg.app.SliderField", although SliderField itself is not Mapped in any way, but is only transferred to .persist after explicit conversion to Field

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Smirnov, 2016-12-22
@bobzer

If I understand correctly, all classes are projected onto one table. At the same time, Field contains projections of those fields of the table that are used in all classes, and Field's heirs project additional fields specific to them. In this case, the following construction should be suitable:

@MappedSuperclass
abstract class Field {
//...
}

@Entity
@Table(name = "FIELDS")
class SliderField extends Field {
//...
}

@Entity
@Table(name = "FIELDS")
class DateField extends Field {
//...
}

Your main mistake is that the @Entity annotation should mark final classes, not parent ones. Intermediate parent classes should be marked as @MappedSuperclass.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question