0

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
Galya
  • 6,294
  • 6
  • 27
  • 45
  • the discriminator is a bit pointless, since each class has its own table and hance any table is not shared. – Neil Stockton Mar 20 '16 at 18:21
  • @NeilStockton At this point it probably is, but what about when another class extends the Person class which is going to happen? – Galya Mar 20 '16 at 19:01
  • That class is then going to have its own table also. An inheritance (in JPA) strategy applies to ALL classes in the tree – Neil Stockton Mar 20 '16 at 19:07

2 Answers2

0

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
Community
  • 1
  • 1
SkyWalker
  • 28,384
  • 14
  • 74
  • 132
  • I don't have the annotation @Id on the subclass, just a id field in the Table called APPLICANT_ID. Otherwise how is the connection between the tables going to be managed? – Galya Mar 20 '16 at 18:39
  • @Galya please check GenerationType.TABLE instead of GenerationType.SEQUENCE – SkyWalker Mar 20 '16 at 18:46
  • I need the Id to be generated by the sequence, since my persistence works with Oracle DB. – Galya Mar 20 '16 at 18:57
  • Would you please add Person and Applicant class in details @Galya – SkyWalker Mar 20 '16 at 19:02
  • There is absolutely nothing else related, only fields annotated with @Column - name, email, phone, etc... No other relations, joins or ids. – Galya Mar 20 '16 at 19:18
0

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.
Galya
  • 6,294
  • 6
  • 27
  • 45