Java 在hibernate中何时使用DiscriminatorValue注释

Java 在hibernate中何时使用DiscriminatorValue注释,java,hibernate,jpa,persistence,Java,Hibernate,Jpa,Persistence,在hibernate中使用discriminator value annotation的最佳方案是什么以及什么时候?当您使用单表策略进行实体继承,并且希望discriminator列的值不是实体具体类的类名称,或者当鉴别器列的类型不是字符串时 这在中以一个例子进行了解释。这两个链接帮助我最了解继承概念: 要理解鉴别器,首先必须理解继承策略:单表、联接、表/类 鉴别器通常用于单表继承,因为您需要一列来标识记录的类型 示例:您有一个班级学生和两个子班级:GoodStudent和BadStuden

在hibernate中使用discriminator value annotation的最佳方案是什么以及什么时候?

当您使用单表策略进行实体继承,并且希望discriminator列的值不是实体具体类的类名称,或者当鉴别器列的类型不是字符串时


这在中以一个例子进行了解释。

这两个链接帮助我最了解继承概念:

要理解鉴别器,首先必须理解继承策略:单表、联接、表/类

鉴别器通常用于单表继承,因为您需要一列来标识记录的类型

示例:您有一个班级学生和两个子班级:GoodStudent和BadStudent。好的和坏的学生数据都将存储在一个表中,但我们当然需要知道类型,以及(DiscriminatorColumn和)DiscriminatorValue的输入时间

注释学生课堂

@Entity
@Table(name ="Student")
@Inheritance(strategy=SINGLE_TABLE)
@DiscriminatorColumn(discriminatorType = DiscriminatorType.STRING,
    name = "Student_Type")
public class Student{
     private int id;
     private String name;
}
@Entity
@DiscriminatorValue("Bad Student")
public class BadStudent extends Student{ 
 //code here
}
@Entity
@DiscriminatorValue("Good Student")
public class GoodStudent extends Student{ 
//code here
}
坏学生班

@Entity
@Table(name ="Student")
@Inheritance(strategy=SINGLE_TABLE)
@DiscriminatorColumn(discriminatorType = DiscriminatorType.STRING,
    name = "Student_Type")
public class Student{
     private int id;
     private String name;
}
@Entity
@DiscriminatorValue("Bad Student")
public class BadStudent extends Student{ 
 //code here
}
@Entity
@DiscriminatorValue("Good Student")
public class GoodStudent extends Student{ 
//code here
}
好学生班

@Entity
@Table(name ="Student")
@Inheritance(strategy=SINGLE_TABLE)
@DiscriminatorColumn(discriminatorType = DiscriminatorType.STRING,
    name = "Student_Type")
public class Student{
     private int id;
     private String name;
}
@Entity
@DiscriminatorValue("Bad Student")
public class BadStudent extends Student{ 
 //code here
}
@Entity
@DiscriminatorValue("Good Student")
public class GoodStudent extends Student{ 
//code here
}
因此,现在Student表将有一个名为Student\u Type的列,并将学生的鉴别器值保存在其中

-----------------------
id|Student_Type || Name |
--|---------------------|
1 |Good Student || Ravi |
2 |Bad Student  || Sham |
-----------------------

参见上面所示的链接。

< P>这里是对每个类层次结构的Hibernate表的说明和一个示例,请考虑我们称为Bayes的基类和2个派生类,如信用卡、支票

如果我们保存派生类对象,如信用卡或支票,则自动付款类对象也将保存到数据库中,并且在数据库中,所有数据将仅存储到单个表中,这肯定是基类表


但是在这里,我们必须在数据库中使用一个额外的鉴别器列,只是为了确定表中保存了哪个派生类对象以及基类对象,如果我们不使用此列,hibernate将抛出异常让我用一个示例向您解释。假设你们有一个叫做Animal的类,在Animal类下面有很多子类,比如爬行动物、鸟类等等

在数据库中有一个名为
ANIMAL

---------------------------
ID||NAME      ||TYPE     ||
---------------------------
1 ||Crocodile ||REPTILE  ||
---------------------------
2 ||Dinosaur  ||REPTILE  ||
---------------------------
3 ||Lizard    ||REPTILE  || 
---------------------------
4 ||Owl       ||BIRD     ||
---------------------------
5 ||parrot    ||BIRD     ||
---------------------------
这里的
TYPE
列被称为DiscriminatorColumn,因为该列包含清楚区分爬行动物和鸟类的数据。而
类型
列中的数据
爬行动物
鸟类
是鉴别器值

因此,在java部分中,该结构将如下所示:

动物类别:

@Getter
@Setter
@Table(name = "ANIMAL")
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(discriminatorType = DiscriminatorType.STRING, name = "TYPE")
public class Animal {

    @Id
    @Column(name = "ID")
    private String id;

    @Column(name = "NAME")
    private String name;

}
爬行动物类:

@Entity
@DiscriminatorValue("REPTILE")
public class Reptile extends Animal {

}
鸟类种类:

@Entity
@DiscriminatorValue("BIRD")
public class Bird extends Animal {

}

您是否已经阅读了文档,可以参考&java7javadoc声明DiscriminatorColumn可以被连接策略和单个表使用。