【问题标题】:Table-per-hierarchy strategy without a discriminator column in Hibernate在 Hibernate 中没有鉴别器列的逐层表策略
【发布时间】:2014-04-19 07:18:43
【问题描述】:

我们有一个旧表,我们不想在其中添加鉴别器列。 是否可以在没有鉴别器列的情况下拥有每个层次结构的表?

我的父类代码:

@Entity
@Table(name = "SHAPE1")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "Discriminator",
                     discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue(value = "S")

public class Shape {

    @Id
    @Column(name = "Shape_Id")
    int shapeId;

    @Column(name = "Shape_Name")
    String shapeName;

    public Shape() {

    }

    public Shape(int id, String shapeName) {
        this.shapeId = id;
        this.shapeName = shapeName;
    }

    // getters and setters
}

我的孩子班:

@Entity
@DiscriminatorValue(value = "R")
public class Rectangle extends Shape {

    @Column(name = "Rectangle_Length")
    int length;

    @Column(name = "Rectangle_Breadth")
    int breadth;

    public Rectangle() {
    }

    public Rectangle(int id, String shapeName, int length, int breadth) {
        super(id, shapeName);
        this.length = length;
        this.breadth = breadth;
    }

    // getters and setters
}

你可以在我上面的例子中看到,我必须使用一个鉴别器。

【问题讨论】:

    标签: java hibernate inheritance


    【解决方案1】:

    SINGLE_TABLE 是一种非常强大的策略,但有时,特别是对于遗留系统,您无法添加额外的鉴别器列。为此,Hibernate 引入了判别器公式的概念:@DiscriminatorFormula@DiscriminatorColumn 的替代品,并使用 SQL 片段作为判别器解析的公式(无需专门的列)。

    @Entity
    @DiscriminatorFormula("case when forest_type is null then 0 else forest_type end")
    public class Forest { ... }
    

    【讨论】:

      猜你喜欢
      • 2019-02-17
      • 2011-09-06
      • 1970-01-01
      • 2011-05-11
      • 1970-01-01
      • 1970-01-01
      • 2015-02-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多