我可以让Hibernate通过工厂方法创建一个对象吗?

Bri*_*ent 14 java orm hibernate factory

有没有办法在Hibernate中映射工厂方法(而不是让Hibernate调用默认构造函数并反射设置属性或字段)?

如果无法映射,Hibernate是否为逐个类的自定义对象创建提供了一个钩子?

谢谢!

Pas*_*ent 10

这可以使用以下两者之一:

我认为Interceptor方法更容易.这是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;
Run Code Online (Sandbox Code Playgroud)


Ber*_*t F 0

请参阅Hibernate 和 Spring 事务 - 使用私有构造函数/静态工厂方法,但不是避免“反射设置属性或字段”部分的解决方案。