我可以让Hibernate通过工厂方法创建一个对象吗? [英] Can I have Hibernate create an object through factory method?

查看:92
本文介绍了我可以让Hibernate通过工厂方法创建一个对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以在Hibernate中映射工厂方法(与使Hibernate调用默认构造函数并反射设置属性或字段相反)?

如果它不能被映射,Hibernate是否提供了一个钩子来自定义对象创建类的基础?



谢谢!

>解决方案

这是可行的使用:





我认为Interceptor方法更容易。这里是 Interceptor.instantiate()

  / ** 
*实例化实体类。返回< tt> null< / tt>以表明Hibernate应该使用
*类的默认构造函数。返回的实例
*的标识符属性应该使用给定的标识符进行初始化。
*
* @param entityName实体的名称
* @param entityMode要返回的实体实例的类型。
* @param id新实例的标识符
* @return类的一个实例,或者< tt> null< / tt>选择默认行为
* /
public Object instantiate(String entityName,EntityMode entityMode,Serializable id)throws CallbackException;


Is there a way to map a factory method in Hibernate (as opposed to having Hibernate call a default constructor and reflectively set properties or fields)?

And if it can't be mapped, does Hibernate provide a hook for custom object creation on a class by class basis?

Thanks!

解决方案

This is doable using either:

I think the Interceptor approach is easier. Here is the javadoc of the 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通过工厂方法创建一个对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆