The problem with two levels of inheritance in a sleeping display

Here is my class structure:

class A class B extends A class C extends A class D extends C class E extends C 

And here are my mappings (class bodies are omitted for brevity):

Grade A:

 @Entity @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @MappedSuperclass @DiscriminatorColumn( name="className", discriminatorType=DiscriminatorType.STRING ) @ForceDiscriminator public abstract class A 

Grade B:

 @Entity @DiscriminatorValue("B") public class B extends A 

Grade C:

 @Entity @DiscriminatorValue("C") @MappedSuperclass @DiscriminatorColumn( name="cType", discriminatorType=DiscriminatorType.STRING ) @ForceDiscriminator public abstract class C extends A 

Class D:

 @Entity @DiscriminatorValue("D") public class D extends C 

Class E:

 @Entity @DiscriminatorValue("E") public class E extends C 

I have a class F that contains set A:

 @Entity public class F { ... @OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL) @JoinTable( name="F_A", joinColumns = @JoinColumn(name="A_ID"), inverseJoinColumns = @JoinColumn(name="F_ID") ) private Set<A> aSet = new HashSet<A>(); ... 

The problem is that whenever I add a new E-instance to aSet and then call session.saveOrUpdate(fInstance) , hibernate saves "A" as the discriminator string. When I try to access aSet in an instance of F, I get the following exception (full stacktrace is omitted for brevity):

 org.hibernate.InstantiationException: Cannot instantiate abstract class or interface: path.to.class.A 

Am I comparing classes incorrectly? How should I display multiple levels of inheritance?

Thanks for the help!

+4
source share
1 answer

I found a solution, so I think I will post it here if someone encounters this problem.

It turns out that additional annotations in class C are causing the problem. When I got rid of everything except the creature, and left all the other classes as they were (with the corresponding discriminator values), everything worked correctly.

 @Entity public class C extends A ... 
+3
source

Source: https://habr.com/ru/post/1310300/


All Articles