Red Hat Training

A Red Hat training course is available for Red Hat JBoss Web Server

4.3. オブジェクトのロード

エンティティマネージャの find() メソッドを使用して ID 値別にエンティティインスタンスをロードします。
cat = em.find(Cat.class, catId);

// You may need to wrap the primitive identifiers
long catId = 1234;
em.find( Cat.class, new Long(catId) );
場合によっては、オブジェクトステータスをロードせずに、参照だけしたい (プロキシ) 場合があります。この参照は、getReference() メソッドを使用して取得できます。これは、親をロードせずに親に子をリンクする場合に特に役に立ちます。
child = new Child();
child.SetName("Henry");
Parent parent = em.getReference(Parent.class, parentId); //no query to the DB
child.setParent(parent);
em.persist(child);
em.refresh() 操作を使用してエンティティインスタンスとそのコレクションをいつでもリロードできます。これは、エンティティの一部のプロパティを初期化するためにデータベーストリガが使用される場合に役に立ちます。関係のカスケードスタイルとして REFRESH を指定しない限り、エンティティインスタンスとそのコレクションのみが更新されます。
em.persist(cat);
em.flush(); // force the SQL insert and triggers to run
em.refresh(cat); //re-read the state (after the trigger executes)