【问题标题】:Hibernate 3.3 Inheritance @DiscriminatorColumn in @IdClassHibernate 3.3 在@IdClass 中继承@DiscriminatorColumn
【发布时间】:2012-12-02 15:57:22
【问题描述】:

id 类中的鉴别器列存在继承问题。该表将成功创建,但每个条目在描述符列中获得“0”值。

这是我的基类:

@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;

....
}

这是我的基础 pk 类:

@Embeddable
public static class BasePK implements Serializable {

@ManyToOne
protected Test test;

@ManyToOne
protected Test2 test2;

@Column(nullable = false)
protected int type;

...
}

我有几个这样的子类:

@Entity
@DiscriminatorValue("1")
@SuppressWarnings("serial")
public class Child extends Base {

}

因此,如果我坚持一个新的 Child 类,我希望将“1”作为类型,但我得到“0”。当我从 BasePK 类中删除类型并直接添加到我的 Base 类中时,它可以工作。但是类型应该是键的一部分。

任何帮助将不胜感激。

【问题讨论】:

    标签: java sql hibernate jpa persistence


    【解决方案1】:

    我做了一些改变,

    我跳过了额外的可嵌入类,因为它们是相同的。

    我必须在注解和子类的构造函数中设置类型值,否则休眠会话无法处理具有相同值的不同类(得到 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;
        }
    }
    

    【讨论】:

    • 谢谢。像魅力一样工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-25
    • 1970-01-01
    • 1970-01-01
    • 2017-08-10
    • 1970-01-01
    • 2015-08-20
    相关资源
    最近更新 更多