javahibernate

org.hibernate.mapping.SingleTableSubclass cannot be cast to org.hibernate.mapping.RootClass 2 tables, 3 classes


I have an error:

java.lang.ClassCastException: org.hibernate.mapping.SingleTableSubclass cannot be cast to org.hibernate.mapping.RootClass

It's thrown because I have two @Id annotations. I have abstract class:

@Entity
@Table(name = "PRODUCTS")
public abstract class CommonProduct<Type extends AbstractProductUnderlying> extends HibernatePersistentObject implements Serializable, UniqueObject {
    @Ignore
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "products_seq")
    @SequenceGenerator(name = "products_seq", sequenceName = "products_seq")
    @Column(name = "productId")
    protected Integer productId;

I have second class without @Id which extends the one above and it's the same table:

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Product extends CommonProduct<ProductUnderlying> {

and there is third table which has different Id as CommonProduct:

@Entity(name = "viewProductHistory")
@Table(name = "PRODUCT_HISTORY")
public class ProductHistory extends CommonProduct<ProductUnderlyingHistory> {

    @Id
    @Column(name = "PRODUCT_HISTORY_ID")
    private int ProductHistoryId;

How can I solve it if two classes extends the one with @Id but one of them has different Id?


Solution

    1. Replace
    @Entity
    @Table(name = "PRODUCTS")
    

    on CommonProduct with

    @MappedSuperclass
    
    1. Remove @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
    2. Put @Table(name = "PRODUCTS") where it belongs. Next to Product
    3. Move productId from CommonProduct to Product