Jin Kwon
Jin Kwon

Reputation: 21978

intermediate `@MappedSuperclass` failed to persist

I have three classes look like this.

@Entity
class A {

    @Id @GeneratedValue @TableGenerator
    private long id;
}

@MappedSuperclass
abstract class B extends A {
}

@Entity
class C extends B {
}

Should above even work? And it seems not work, at least, with EclipseLink.

I got Column 'ID' cannot be null when I tried to persist an instance of C.

I found a bug at Inheritance with abstract intermediate class using @MappedSuperclass fails to populate subclasses but I'm not sure it is exactly the same situation or not.

UPDATE per @James's answer

I'm sorry, I should written more verbosely. Yes I'm intending SINGLE_TABLE inheritance. I don't have any extended property with B nor C. It's just a hierarchical class design.

@DiscriminatorColumn
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Entity
abstract class A {

    @Id @GeneratedValue @TableGenerator
    private long id;
}

@DiscriminatorValue("B") @Entity // I currently should do like this.
//@MappedSuperclass              // error
abstract class B<T extends SomeoneElse> extends A {
}

@DiscriminatorValue("C")
@Entity
class C extends B<James> {
}

Upvotes: 0

Views: 517

Answers (1)

James
James

Reputation: 18379

I believe that in JPA a @MappedSuperclass must be a superclass not a subclass (as its name infers).

I'm not sure what having an @Entiy subclass as a @MappedSuperclass would mean?

What are you trying to do?

For @Entity inheritance JPA only provides three options, SINGLE_TABLE, JOINED, and TABLE_PER_CLASS. All persistence subclasses must be entities.

I assume you are using JOINED inheritance and trying to avoid a table for B. JPA does not specify a standard way of doing this. In EclipseLink you can avoid the table by making its table match the parent (@Table(name="A")).

Upvotes: 1

Related Questions