在一对一的每个具体类表结构中创建外键

drv*_*ijk 23 java orm hibernate

TL; DR如何强制Hibernate模式创建,以便在每个具体的表设置AbstractProperty.ownerIdOwner.ownerId为下面显示的结构创建外键约束,而不向?添加Owner属性AbstractProperty

我正在开发一个项目,我有以下类结构:

类结构样本

Owner与a的一对一映射AbstractProperty,由ConcreteProperty类扩展(和其他类似的AnotherProperty,但这与此问题的其余部分并不相关).

AbstractProperty实际上只有一个属性的abstractPropertyId.为此,我们要使用的表,每具体类结构,表结束了Owner,ConcreteProperty和表格的其他AbstractProperty扩展类(AnotherProperty).

为此,我创建了以下映射Owner:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.example">
    <class name="Owner">
        <id name="ownerId">
            <generator class="identity"/>
        </id>
        <property name="ownerProperty"/>
        <one-to-one name="abstractProperty"/>
    </class>
</hibernate-mapping>
Run Code Online (Sandbox Code Playgroud)

对于AbstractProperty:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.example">
    <class name="AbstractProperty" abstract="true">
        <id name="ownerId">
            <generator class="foreign">
                <param name="property">ownerId</param>
            </generator>
        </id>
        <union-subclass name="ConcreteProperty">
            <property name="concreteProperty"/>
        </union-subclass>
        <union-subclass name="AnotherProperty">
            <property name="anotherProperty"/>
        </union-subclass>
    </class>
</hibernate-mapping>
Run Code Online (Sandbox Code Playgroud)

这有效.

但是,这是我的问题,使用此映射并让Hibernate为me(<property name="hbm2ddl.auto">create</property>)创建模式,它不会创建从ConcreteProperty.ownerId数据库字段到Owner.ownerId字段的外键约束.它,当我创建逆约束从一到一个场AbstractProperty,以Owner使用此映射AbstractProperty(其中owner字段的类型是OwnerAbstractPropertyJava类):

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.example">
    <class name="AbstractProperty" abstract="true">
        <id name="ownerId">
            <generator class="foreign">
                <param name="property">ownerId</param>
            </generator>
        </id>
        <one-to-one name="owner" constrained="true"/>
        <union-subclass name="ConcreteProperty">
            <property name="concreteProperty"/>
        </union-subclass>
        <union-subclass name="AnotherProperty">
            <property name="anotherProperty"/>
        </union-subclass>
    </class>
</hibernate-mapping>
Run Code Online (Sandbox Code Playgroud)

如何从强制外键的创建AbstractProperty.ownerIdOwner.ownerId没有这个Owner在我的领域AbstractProperty

And*_*i I 0

我们使用标准 JPA(没有特定于 hibernate 的 hack)并且遇到了同样的问题,并且我们没有找到一个好的解决方案。

假设:

  1. AbstractProperty是包中的一个类,可在不同的应用程序中重用/共享,并且您不希望引用特定于应用程序的Owner类。
  2. ConcreteProperty&AnotherProperty是特定于应用程序的。

在这种情况下,解决方案是将引用放入ConcretePropertyOwner使用外键)中,最终两者都AnotherProperty扩展相同的ApplicationProperty,并设为abstractPropertyId私有,以便在设置所有者时自动设置。