java - Hibernate 3.3 继承@IdClass 中的@DiscriminatorColumn

标签 java sql hibernate jpa persistence

我对 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 {

}

因此,如果我保留一个新的子类,我希望类型为“1”,但我得到的是“0”。当我从 BasePK 类中删除类型并直接添加到我的基类中时,它会起作用。但类型应该是 key 的一部分。

如有任何帮助,我们将不胜感激。

最佳答案

我做了一些改变,

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

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

关于java - Hibernate 3.3 继承@IdClass 中的@DiscriminatorColumn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13882242/

相关文章:

java - Maven+JDK9 模块 : Failed to parse module-info

java - 在字符串上使用 line.split 时获取 ArrayIndexOutOfBound

sql - PostgreSQL 唯一索引复合键

php - 需要有关 SQL 的帮助以对搜索结果进行排名

java - Spring-Data 中子对象不与父对象一起获取

java - 为什么 Hibernate 5.2 支持 java.time.Instant 但 JPA 2 不支持?

java - Java 中的参数化类型 (GADT)

JavaFX2 : How to retrieve Task in Service's onFailed event?

sql - 提高 PostgreSQL 中的查询速度

java - HQL 查询从错误的类中获取属性