Hibernate 继承映射中的多个 DiscriminatorColumn

发布于 2024-12-19 02:00:05 字数 621 浏览 0 评论 0原文

我了解了如何在 Hibernate 继承映射中使用 DiscriminatorColumn, 然而,在我的场景中,我有更复杂的继承模块,我需要定义两个鉴别器。我想为整个继承树使用一张表。 基本上,父类是抽象的,它代表一个人实体, 然后,我有它的两个不同的抽象实现,Wife 和 Wife 。丈夫。所以基本上我已经需要一个鉴别器了。那么对于每个(妻子和丈夫)我都有不同的消息需要保留,所以我不需要为每个(妻子和丈夫)创建一个鉴别器。

我想出了这个实现,但我陷入困境,我该如何从这里继续?

@Entity
@Table (name="sex")
@Inheritance (strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn (name="transaction_type", discriminatorType=DiscriminatorType.STRING)
public abstract class Person {
...
}

@Entity
@DiscriminatorValue("wife")
public class Wife extends Person {
...
}

现在,我如何在 Wife 类上放置一个鉴别器列,就像我对 Parent 所做的那样?

I understood how to use DiscriminatorColumn in Hibernate inheritance mapping,
However, in my scenario, I have more complicated inheritance module, where I need to define two Discriminators. I want to use one table for the entire inheritance tree.
Basically, the parent class is abastract, and it represents a person entity,
then, I have two different abstract implementation of it, Wife & Husband. so basically I already need one discriminator. then for each (Wife & Husband) I have different messages that needs to be persisted, so I need no to create a discriminator for each (Wife & Husband).

I came up with this implementation, but I'm stuck, how do I continue from here ?

@Entity
@Table (name="sex")
@Inheritance (strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn (name="transaction_type", discriminatorType=DiscriminatorType.STRING)
public abstract class Person {
...
}

@Entity
@DiscriminatorValue("wife")
public class Wife extends Person {
...
}

now, how do I put a discriminatorcolumn on the Wife Class, the same as I did with the Parent ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北方的韩爷 2024-12-26 02:00:05

你不需要第二个鉴别器。 Wife 实体将正常工作。
在二级继承的情况下,中产阶级(在这种情况下是妻子和丈夫)不需要有@DiscriminatorColumn
相关问题2级继承的问题可能乐于助人。
另一个问题Hibernate Inheritance Single_table

You don't need the seccond discriminator. the Wife Entity will work correctly.
In 2-level inheritance situations, the middle classes (wife and husband in this case) does not nead to have @DiscriminatorColumn.
the related question Problem with 2 levels of inheritance may be helpful.
the other question Hibernate Inheritance Single_table

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文