TL;DRにプロパティを追加せずに、Hibernate スキーマの作成を強制して、コンクリートクラスごとのテーブル設定で外部キー制約を作成するにはどうすればよいAbstractProperty.ownerId
ですか?Owner.ownerId
Owner
AbstractProperty
次のクラス構造を持つプロジェクトに取り組んでいます。
には、クラスによって拡張されるOwner
への 1 対 1 のマッピングがあります (および のようなものもありますが、この質問の残りの部分にはあまり関係ありません)。AbstractProperty
ConcreteProperty
AnotherProperty
には、AbstractProperty
実際には 1 つのプロパティしかありませんabstractPropertyId
。そのため、table-per-concrete-class構造を使用して、最終的に、他の拡張クラス ( ) の table Owner
、ConcreteProperty
、および table を使用します。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>
そしてのために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>
これは機能します。
ただし、これが私の質問です。このマッピングを使用し、Hibernate にスキーマを作成させます ( )。データベース フィールドからフィールドへの<property name="hbm2ddl.auto">create</property>
外部キー制約は作成されません。このマッピングを使用してからへの逆制約付き 1 対 1 フィールドを作成すると、次のようになります(フィールドの型はJava クラスです)。ConcreteProperty.ownerId
Owner.ownerId
AbstractProperty
Owner
AbstractProperty
owner
Owner
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>
<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>
でこのフィールドAbstractProperty.ownerId
を使用せずに外部キーの作成を強制するにはどうすればよいですか?Owner.ownerId
Owner
AbstractProperty