简而言之:有没有办法用两列唯一约束编写一个hibernate xml映射,其中唯一约束的一个字段是抽象类映射的一部分而另一个字段是在子类映射中定义的? / p>
长版本:我有一个类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唯一约束?