简而言之:是否有一种方法可以编写具有两列唯一约束的hibernate xml映射,其中唯一约束的一个字段是抽象类映射的一部分,而另一个字段在子类映射中定义?
长版本:我有一个类AbstractCustomFieldValue
引用其他两个类,一些实体RefType
和另一个实体CustomField
。AbstractCustomFieldValue
有如下所示的几种实现。
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>
refObjekt
和customField
的组合必须唯一。有办法实现这个映射吗?
我仍然可以选择在没有hibernate的数据库中定义一个唯一的键,或者从抽象映射中删除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唯一的约束?
您可以使用