gpt4 book ai didi

带有鉴别器的 hibernate 映射

转载 作者:行者123 更新时间:2023-12-04 07:15:04 29 4
gpt4 key购买 nike

我有一个表,其中一个字段可以指向其他 3 个表之一中的外键,具体取决于鉴别器值是什么(Project、TimeKeep 或 CostCenter。通常这是用子类实现的,我想知道我有什么 注意子类名称与父类相同,并且 noteObject 属性映射到 java.lang.Object 类型的实例变量 所以它应该接受 Project、TimeKeep 或 CostCenter 对象只要我们转换为正确的类型。hibernate 允许这样做吗?谢谢。

<hibernate-mapping package="com.tlr.finance.mappings">

<class name="AdminNotes" table="admin_notes">
<id name="adminNoteId" column="admin_note_id" type="integer">
<generator class="identity" />
</id>

<discriminator column="note_type" type="string" />

<!-- make this property an enumerated type. It is the discriminator -->
<property name="adminNoteType" column="note_type" type="string" not-null="true" />
<property name="adminNote" column="note" type="string" not-null="true" />
<property name="adminNoteAdded" column="note_date" type="timestamp"
not-null="true" />

<subclass name="AdminNotes" discriminator-value="project" >
<many-to-one name="noteObject" column="object_id" class="PsData" /><!-- Project -->
</subclass>

<subclass name="AdminNotes" discriminator-value="user" >
<!-- rename timekeep to user -->
<many-to-one name="noteObject" column="object_id" class="Timekeep" /><!-- user -->
</subclass>

<subclass name="AdminNotes" discriminator-value="costCenter" >
<!-- rename timekeep to user -->
<many-to-one name="noteObject" column="object_id" class="CostCenter" /><!-- cost center -->
</subclass>

</class>

</hibernate-mapping>

最佳答案

鉴别器用于在单个表中存储类层次结构。你所拥有的是一个具有多种含义的类。

http://docs.jboss.org/hibernate/core/3.5/reference/en-US/html/mapping.html#mapping-declaration-discriminator

The element is required for polymorphic persistence using the table-per-class-hierarchy mapping strategy and declares a discriminator column of the table. The discriminator column contains marker values that tell the persistence layer what subclass to instantiate for a particular row.



我认为您无法为每个不同的含义使用单个 AdminNote 类。在数据库级别使用鉴别器来帮助区分一个子类和另一个子类——它实际上不是 java 对象模型的一部分。

您需要定义 AdminNote 的多个子类,每个鉴别器值一个。

关于带有鉴别器的 hibernate 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/174748/

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