gpt4 book ai didi

java - Hibernate 继承类上的嵌套鉴别器

转载 作者:行者123 更新时间:2023-11-29 22:42:53 25 4
gpt4 key购买 nike

我有2个MYSQL表

表1

id     bigint auto increment Primary Key
type Enum ('vegetable','fruit')
color Enum ('green','red','yellow')

表2

id     bigint (same as the id in Table 1)
sweet boolean
sour boolean
.. other fields specific to type fruit

现在我正在创建 3 个对象,首先是父类

@Entity
@Configurable
@Table(name = "table1")
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
public class ParentClass {
..
}

现在是蔬菜二等

@Entity
@Configurable
@DiscriminatorValue("vegetable")
@DiscriminatorColumn(name = "color", discriminatorType = DiscriminatorType.STRING)
public class Vegetable extends Parent{
..
}

第三,水果类

@Entity
@Configurable
@SecondaryTables({ @SecondaryTable(name = "table2",
pkJoinColumns = { @PrimaryKeyJoinColumn(name = "id", referencedColumnName = "id") }) })
@DiscriminatorValue("fruit")
@DiscriminatorColumn(name = "color", discriminatorType = DiscriminatorType.STRING)
public class Fruit extends Parent{
..
}

我需要第二个鉴别器在蔬菜和水果上添加更多继承类(6 个其他类),例如,

@Entity
@Configurable
@DiscriminatorValue("red")
public class RedVegetable extends Vegetable{
..
}

@Entity
@Configurable
@DiscriminatorValue("green")
public class GreenFruit extends Fruit{
..
}

等等。

Hibernate 不允许我这样做。我的设计有什么问题吗?提前致谢!

最佳答案

了解到这在 Hibernate 中无法完成。因此找到了一种替代方法,通过合并 table1 中的判别器 friend ,例如 Enum('fruit|red'、'fruit|green'、'vegetable|red'.. 等)。如果我错了,请纠正我。

关于java - Hibernate 继承类上的嵌套鉴别器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29239670/

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