【问题标题】:How to map a multiple column unique contraint in hibernate with an abstract class field and a subclass field如何在休眠中使用抽象类字段和子类字段映射多列唯一约束
【发布时间】:2015-12-05 05:03:09
【问题描述】:

简而言之:有没有办法编写具有两列唯一约束的休眠 xml 映射,其中唯一约束的一个字段是抽象类映射的一部分,而另一个字段在子类映射中定义?

长版:我有一个类 AbstractCustomFieldValue,它引用了另外两个类,一些实体 RefType 和另一个实体 CustomFieldAbstractCustomFieldValue 有几种实现方式,如下所示。

public abstract class AbstractCustomFieldValue<RefType extends SomeInterface>
{
    protected long id;
    protected RefType refObjekt;
    protected CustomField customField;
    protected String value;
}

public class MyEntityCustomFieldValue extends AbstractCustomFieldValue<MyEntity>
{
}

public class MyOtherEntityCustomFieldValue extends AbstractCustomFieldValue<MyOtherEntity>
{
}

AbstractCustomFieldValue 被映射为抽象类,实现被映射为子类。

<class name="AbstractCustomFieldValue" abstract="true">
    <id name="id">
        <generator class="MyUniqueIdGenerator"/>
    </id>
    <many-to-one name="customField" class="CustomField" column="customfield_id"/>
    <property name="value" length="65535"/>
</class>

<union-subclass name="MyEntityCustomFieldValue" extends="AbstractCustomFieldValue" table="my_entity_customfield_values">
    <many-to-one name="refObjekt" class="MyEntity" column="ref_id"/>
</union-subclass>

<union-subclass name="MyOtherEntityCustomFieldValue" extends="AbstractCustomFieldValue" table="my_other_entity_customfield_values">
    <many-to-one name="refObjekt" class="MyOtherEntity" column="ref_id"/>
</union-subclass>

refObjektcustomField 的组合必须是唯一的。有没有办法通过这种映射来实现这一点?

我仍然可以选择在没有休眠的情况下在数据库中定义唯一键,或者从抽象映射中删除 customField 并将其放入子类映射中:

<properties name="myUniqueKey" unique="true">
    <many-to-one name="customField" class="CustomField" column="customfield_id"/>
    <many-to-one name="refObjekt" class="MyEntity" column="ref_id"/>
</properties>

但是有没有办法在抽象类的映射中保留customField 并且仍然能够定义休眠唯一约束?

【问题讨论】:

    标签: hibernate inheritance hbmxml


    【解决方案1】:

    您可以使用<database-object> 在 hbm 映射中定义任何自定义 DDL。您可以在那里创建独特的约束; hbm2ddl 将执行它。

    【讨论】:

      猜你喜欢
      • 2018-01-07
      • 1970-01-01
      • 1970-01-01
      • 2011-07-19
      • 2020-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多