org.hibernate.WrongClassException on saving an entity via Hibernate

后端 未结 2 1636
一整个雨季
一整个雨季 2021-01-23 02:58

In this question I am working with Hibernate 4.3.4.Final and Spring ORM 4.1.2.RELEASE.

I have an User class, that holds a Set of CardInstances like this:



        
相关标签:
2条回答
  • 2021-01-23 03:29

    According to the first paragraph of the JavaDocs for @ManyToOne:

    It is not normally necessary to specify the target entity explicitly since it can usually be inferred from the type of the object being referenced.

    However, in this case, @ManyToOne is on a field whose type is generic and generic type information gets erased at the type of compilation. Therefore, when deserializing, Hibernate does not know the exact type of the field.

    The fix is to add targetEntity=Card.class to @ManyToOne. Since Card is abstract and has @Inheritance and @DiscriminatorColumn annotations, this forces Hibernate to resolve the actual field type by all possible means. It uses the discriminator value of the Card table to do this and generates the correct class instance. Plus, type safety is retained in the Java code.


    So, in general, whenever there is the chance of a field's type not being known fully at runtime, use targetEntity with @ManyToOne and @OneToMany.

    0 讨论(0)
  • 2021-01-23 03:36

    I solved the problem.

    The root cause lies in this design:

    @Table
    @Entity
    @Inheritance
    @DiscriminatorColumn(name = "card_type", discriminatorType = DiscriminatorType.STRING)
    public class CardInstance<T extends Card> {  
        protected T card;
    }
    
    @Entity
    @DiscriminatorValue("leader")
    public class LeaderCardInstance extends CardInstance<LeaderCard> {
    }
    

    At runtime information about generic types of an class are not present in java. Refer to this question for further information: Java generics - type erasure - when and what happens

    This means hibernate has no way of determining the actual type of the CardInstance class.


    The solution to this is simply getting rid of the generic type and all extending (implementing) classes and just use one class like this:

    @Table
    @Entity
    @Inheritance
    @DiscriminatorColumn(name = "card_type", discriminatorType = DiscriminatorType.STRING)
    public class CardInstance {
        Card card;
    }
    

    This is possible (and by the way the better design) because the member card carries all the information about the card type.


    I hope this helps folk if they run into the same problem.

    0 讨论(0)
提交回复
热议问题