【问题标题】:Multiple DiscriminatorColumn in Hibernate inheritance mappingHibernate继承映射中的多个DiscriminatorColumn
【发布时间】:2011-12-02 20:02:09
【问题描述】:

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

我想出了这个实现,但我卡住了,我该如何从这里继续?

@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 Class 上放置一个 discriminator 列,就像我对 Parent 所做的一样?

【问题讨论】:

  • 为什么需要“第二个鉴别器”?这个不同的消息是另一个 Person 子类的指示符吗?如果是这样 - 你不能创建另一个子类吗?如果没有,您是否可以仅使用常规字段和一些布尔值来指示其中将包含哪种“消息”?
  • @PiotrNowicki,每条消息都是一个不同的类,它包含不同的字段,当然,消息是根据Person的类型来区分的,妻子有自己的消息,丈夫有自己的。
  • 所以WifeHusband 实体不能分别将WifeMessageHusbandMessage 作为字段?
  • 好的,基本概念是这样的,我想要一个叫做“pending-messages”的表。当一个人登录到系统时,它会查看他是否有消息,这是与丈夫双方的一般行为。现在他想从表中检索消息,所以应该也是一个鉴别器列。

标签: hibernate single-table-inheritance


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2012-12-02
    • 2017-12-07
    • 2011-11-22
    • 2010-11-25
    • 2010-09-20
    • 1970-01-01
    • 2014-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多