Reputation: 518
I have a inheritance problem with discriminator column in id class. The table will be created successfull but each entry gets "0" value in descriminator column.
Here is my base class:
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.INTEGER)
@IdClass(BasePK.class)
@SuppressWarnings("serial")
public abstract class Base implements Serializable {
@Id
protected Test test;
@Id
protected Test2 test2;
@Id
private int type;
....
}
Here is my base pk class:
@Embeddable
public static class BasePK implements Serializable {
@ManyToOne
protected Test test;
@ManyToOne
protected Test2 test2;
@Column(nullable = false)
protected int type;
...
}
And I have several subclasses like this:
@Entity
@DiscriminatorValue("1")
@SuppressWarnings("serial")
public class Child extends Base {
}
So if I persist a new Child class I would expect to have "1" as type but I get "0". It works when I remove the type from BasePK class and add directly in my Base class. But the type should be part of the key.
Any help would be greatly appreciated.
Upvotes: 2
Views: 1460
Reputation: 1024
I made some changes,
i skipped the extra embedable class because they were identical.
i had to set the type value in the annotation and in the constructor of the child classes otherwise the hibernate session could not handle different classes with the same value (got a NotUniqueObjectException).
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.INTEGER)
@IdClass(Base.class)
public abstract class Base implements Serializable {
@Id @ManyToOne protected Test test;
@Id @ManyToOne protected Test2 test2;
@Id private int type;
}
@Entity
@DiscriminatorValue("1")
public class Child1 extends Base {
public Child1(){
type=1;
}
}
@Entity
@DiscriminatorValue("2")
public class Child2 extends Base {
public Child2(){
type=2;
}
}
Upvotes: 1