简体   繁体   中英

JPA Inheritance TABLE_PER_CLASS throws MappableContainerException

Something is wrong with the JPA Inheritance and the server throws an error. There are many questions already asked on the topic, but I couldn't find a solution to my problem, so here are the details on what I have so far...

The superclass:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@DiscriminatorColumn(name = "PERSON_TYPE", discriminatorType = DiscriminatorType.INTEGER)
@Table(name = "PERSON")
public class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "TRIGGER_PERSON_PRIMARY_KEY")
    @SequenceGenerator(name = "TRIGGER_PERSON_PRIMARY_KEY", sequenceName = "PERSON_AI_SEQUENCE", allocationSize = 1, initialValue = 1)
    @Column(name = "PERSON_ID", updatable = false, unique = true, nullable = false)
    protected int personId;

The subclass:

@Entity
@Table(name = "APPLICANT")
@DiscriminatorValue(value = "1")
@PrimaryKeyJoinColumn(name="APPLICANT_ID")
public class Applicant extends Person {
}

The error message:

The exception contained within MappableContainerException could not be mapped to a response, re-throwing to the HTTP container

Caused by: java.lang.ClassCastException: org.hibernate.mapping.UnionSubclass cannot be cast to org.hibernate.mapping.RootClass

You can't have @Id in both super class and sub class.

Solution

Remove @Id from sub class Applicant

For more:

  1. Spring 3.1 Hibernate 4 exception for Inheritance [cannot be cast to org.hibernate.mapping.RootClass]
  2. Java/Hibernate JPA: InheritanceType.TABLE_PER_CLASS and IDs

I've finally managed to fix this. There have been several steps I've taken:

  1. Another class extending Person was throwing an error, so I've fixed the dependency.
  2. I've changed the inheritance type to JOINED, which is actually what I'm looking for - different tables for the different columns and one root table for the common stuff.
  3. As a consequence of 2. there was no more necessity of keeping the @PrimaryKeyJoinColumn also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM