gpt4 book ai didi

java - 何时在 hibernate 中使用 DiscriminatorValue 注解

转载 作者:IT老高 更新时间:2023-10-28 20:49:22 25 4
gpt4 key购买 nike

在 hibernate 中使用 DiscriminatorValue 注解的最佳场景和时间是什么时候?

最佳答案

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

http://docs.oracle.com/javaee/6/tutorial/doc/bnbqn.html

http://www.javaworld.com/javaworld/jw-01-2008/jw-01-jpa1.html?page=6

要了解判别器,首先必须了解继承策略:SINGLE_TABLE、JOINED、TABLE_PER_CLASS。

判别器常用于 SINGLE_TABLE 继承,因为您需要一列来标识记录的类型。

示例:您有一个 Student 类和 2 个子类:GoodStudent 和 BadStudent。 Good 和 BadStudent 数据都将存储在 1 个表中,但是我们当然需要知道类型,这就是 (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
}

所以现在 Student 表将有一个名为 Student_Type 的列,并将在其中保存 Student 的 DiscriminatorValue

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

查看我上面发布的链接。

关于java - 何时在 hibernate 中使用 DiscriminatorValue 注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16772370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com