Hibernate でファクトリ メソッドをマップする方法はありますか (Hibernate がデフォルトのコンストラクターを呼び出し、プロパティまたはフィールドを反射的に設定するのとは対照的に)?
また、マップできない場合、Hibernate はクラスごとにカスタム オブジェクトを作成するためのフックを提供しますか?
ありがとう!
これは、次のいずれかを使用して実行できます。
EntityPersister
できます) ~または~Configuration
Interceptor
実装するカスタムInterceptor.instantiate()
インターセプターのアプローチの方が簡単だと思います。の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;
また、マップできない場合、Hibernate はクラスごとにカスタム オブジェクトを作成するためのフックを提供しますか?
エンティティ リスナーを確認してください。これらは、役立つ注釈のみを追加します。@PrePersist または @PostLoad を考えてください。
あなたが求めていることを正確に理解したかどうかはわかりませんが、解決策がここに記載されていると思います (解決策 4 - Hibernate インターセプター、メソッド onLoad を参照): "Spring と Hibernate を使用したドメイン駆動設計" http:// www.jblewitt.com/blog/?p=129
Hibernate and Spring transactions - using private constructors/static factory methods を参照してください。ただし、「反射的に設定されたプロパティまたはフィールド」部分を回避するための解決策ではありません。