Hibernate与鉴别器的映射 [英] Hibernate mappings with a discriminator

查看:77
本文介绍了Hibernate与鉴别器的映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,其中一个字段可以根据描述符的值(Project,TimeKeep或CostCenter)指向其他3个表中的其中一个表中的外键(通常这是用子类实现的,我想知道是否注意,子类名与父类相同,并且noteObject属性映射到java.lang.Object类型的实例变量,所以它应该接受Project, TimeKeep或CostCenter对象,只要我们转换成正确的类型.hibernate会允许这个吗?谢谢。

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

< class name =AdminNotestable =admin_notes>
< id name =adminNoteIdcolumn = admin_note_idtype =integer>
< generator class =identity/>
< / id>

< discriminator column =note_type type =string/>

<! - 使这个属性成为一个枚举类型,它是t他的鉴别器 - >
< property name =adminNoteTypecolumn =note_typetype =stringnot-null =true/>
< property name =adminNoteAddedcolumn =note_datetype =timestamp
not-null =true/>

< subclass name =AdminNotesdiscriminator-value =project>
< / subclass>

< subclass name =AdminNotesdiscriminator-value =user>
<! - 重命名记录到用户 - >
< / subclass>

< subclass name =AdminNotesdiscriminator-value =costCenter>
<! - 重命名记录到用户 - >
< / subclass>

< / class>

< / hibernate-mapping>


解决方案

判别符用于将类层次结构存储在单个表。你在那里有一个具有多重意义的班级。

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


元素是
所需的多态持久性
使用table-per-class-hierarchy
映射策略和声明该表的
鉴别器列。
discriminator列包含标记
值,告诉持久层

特定行实例化哪个子类。


我认为您无法为每种不同的含义使用一个AdminNote类。鉴别器用于数据库级别,以帮助区分一个子类和另一个子类 - 它实际上不是Java对象模型的一部分。



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


I have a table with one field that can point to a foreign key in one of 3 other tables based on what the descriminator value is (Project, TimeKeep, or CostCenter. Usually this is implemented with subclasses, and I am wondering if what I have below will work. Note the subclass name is the same as the parent class and the noteObject property is mapped to an instance variable of type java.lang.Object so it should accept either a Project, TimeKeep or CostCenter object as long as we cast to the correct type. Will hibernate allow this? Thanks.

<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>

解决方案

Discriminators are used for for storing class hierarchies in a single table. What you have there is a single class with multiple meanings.

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.

I don't think you'll be able to use a single AdminNote class for each of those different meanings. The discriminator is used at the database level to help distinguish one subclass from another - it's not actually part of the java object model.

You'll need to define multiple subclasses of AdminNote, one for each discriminator value.

这篇关于Hibernate与鉴别器的映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆