14

Hibernate でファクトリ メソッドをマップする方法はありますか (Hibernate がデフォルトのコンストラクターを呼び出し、プロパティまたはフィールドを反射的に設定するのとは対照的に)?

また、マップできない場合、Hibernate はクラスごとにカスタム オブジェクトを作成するためのフックを提供しますか?

ありがとう!

4

5 に答える 5

10

これは、次のいずれかを使用して実行できます。

インターセプターのアプローチの方が簡単だと思います。のjavadocは次のInterceptor.instantiate()とおりです。

/**
 * Instantiate the entity class. Return <tt>null</tt> to indicate that Hibernate should use
 * the default constructor of the class. The identifier property of the returned instance
 * should be initialized with the given identifier.
 *
 * @param entityName the name of the entity
 * @param entityMode The type of entity instance to be returned.
 * @param id the identifier of the new instance
 * @return an instance of the class, or <tt>null</tt> to choose default behaviour
 */
public Object instantiate(String entityName, EntityMode entityMode, Serializable id) throws CallbackException;
于 2010-11-03T22:54:56.327 に答える
3

UserTypeを見てください。nullSafeGet()でファクトリを呼び出し、すべてのフィールドに自分で入力する必要があります。逆の作業はnullSafeSet()で行われます。

于 2009-06-05T02:21:19.310 に答える
2

また、マップできない場合、Hibernate はクラスごとにカスタム オブジェクトを作成するためのフックを提供しますか?

エンティティ リスナーを確認してください。これらは、役立つ注釈のみを追加します。@PrePersist または @PostLoad を考えてください。

于 2009-06-08T02:40:14.910 に答える
0

あなたが求めていることを正確に理解したかどうかはわかりませんが、解決策がここに記載されていると思います (解決策 4 - Hibernate インターセプター、メソッド onLoad を参照): "Spring と Hibernate を使用したドメイン駆動設計" http:// www.jblewitt.com/blog/?p=129

于 2010-11-13T11:10:28.553 に答える
0

Hibernate and Spring transactions - using private constructors/static factory methods を参照してください。ただし、「反射的に設定されたプロパティまたはフィールド」部分を回避するための解決策ではありません。

于 2009-06-05T01:06:27.810 に答える