// 下階層用テンプレート
#topicpath
----
//ここにコンテンツを記述します。


***推移的に永続化するかどうか [#sfc41241]
以下のようにまだテーブルに登録されていない担当者をつかって顧客に関連づけてみます。

 Session session = sessionFactory.openSession();
 Transaction tx = session.beginTransaction();
 
 User user = new User();
 user.setName("担当者1");
 
 Customer customer = new Customer();
 customer.setName("顧客1");
 customer.setUser(user);
 session.save(customer);
 
 tx.commit();
 session.close();


Customer.hbm.xmlのmany-to-oneの設定が
 <many-to-one name="user" class="nu.mine.kino.entity.User" fetch="select">
というようにカスケード指定がない場合、
 org.hibernate.TransientObjectException: nu.mine.kino.entity.User
という例外が発生します。
 <many-to-one name="user" class="nu.mine.kino.entity.User" fetch="select" cascade="save-update">
とカスケード指定すると、まずは「担当者1」を登録後、「顧客1」を登録しにいきます。「担当者1」を明示的にsaveする必要はないって事ですね。

----
この記事は
#vote(おもしろかった[6],そうでもない[3])
#vote(おもしろかった[6],そうでもない[4])

#comment
#topicpath


SIZE(10){現在のアクセス:&counter;}

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS