私が持っているとします:
-- table_1 -------------------
|id | property_x | data |
------------------------------
-- table_2 ------------------------
|id | property_x | moar_data |
-----------------------------------
そして、それはからへtable_1
のFKを持っています
(なぜIDKではないのですか?このプロジェクトはすでにこのようなものでした:()property_x
table_2.property_x
table_2.id
HBM は次のようになります。
<!-- Table 1 -->
<hibernate-mapping package="com.example">
<class name="Table1" table="table_1">
<id column="id" type="integer" name="id">
<generator class="native"/>
</id>
<many-to-one class="Table2" fetch="join" name="table2" not-null="false">
<column name="property_x" />
</many-to-one>
</class>
</hibernate-mapping>
と
<!-- Table 2 -->
<hibernate-mapping package="com.example">
<class name="Table2" table="table_2">
<id column="id" type="integer" name="id">
<generator class="native"/>
</id>
<property column="property_x" name="propertyX" type="string"/>
</class>
</hibernate-mapping>
問題は、DB に新しい Table2 エントリを作成したり、ロードしたりせずに、 をTable1
使用してオブジェクトを保存したいということです。session.save(objTable1)
私は過去に新しいオブジェクトを作成し、主キーの値のみを設定し、他のすべてを空白のままにし、Table2
テーブルを更新させないことでこれを行いましたが、これは PK >_< ではありません。
ここでtable_2
、エントリwith id=5
に ,があるとしproperty_x="EX"
ます。私が次のことをすると...
// example method
public void saveSomething(Sessions sess) {
Table1 table1 = new Table1();
Table2 table2 = new Table2();
table2.setPropertyX("EX");
table1.setTable2(table2);
sess.save(table1);
}
...新しいエントリを作成します。これは、PK (id)Table2
が設定されていないためだと思います。
私の質問は、hibernate が FK オブジェクトから DB に新しいエントリを作成することをどのように決定するかということです.HBM ファイルでそれを回避する方法はありますか?