2013年11月23日土曜日

HibernateのAutomatic State Detection

まだちょっとsaveOrUpdateとmergeの違いを分かっていないのですが、基本的なところでsaveする時とupdateする時の違いは、エンティティのID(主キー)がnullかどうかで自動制御しているようです。ちょっと、ここもハマった。

// in the first session
Cat cat = (Cat) firstSession.load(Cat.class, catID);

// in a higher tier of the application
Cat mate = new Cat();
cat.setMate(mate);

// later, in a new session
secondSession.saveOrUpdate(cat);   // update existing state (cat has a non-null id)
secondSession.saveOrUpdate(mate);  // save the new instance (mate has a null id)


http://docs.jboss.org/hibernate/core/3.3/reference/en/html/objectstate.html#objectstate-saveorupdate

0 件のコメント:

コメントを投稿