Hibernate和Spring事务 - 使用私有构造函数/静态工厂方法 [英] Hibernate and Spring transactions - using private constructors/static factory methods

查看:87
本文介绍了Hibernate和Spring事务 - 使用私有构造函数/静态工厂方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个拥有以下Spring Bean的Hibernate / Spring应用程序:

 < bean id =transactionManagerclass =org.springframework.orm.hibernate3.HibernateTransactionManager/> 
< bean id =sessionFactoryclass =org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean/>

将应用程序连接在一起时,在hibernate实体中使用私有构造函数时会出现以下错误: p>

 调用init方法失败;嵌套的异常是java.lang.IllegalArgumentException:类'ourclass'中没有可见的构造函数

域对象,例如Employee或类似的。

当将构造函数的可见性修饰符更改为包(或公共)时,应用程序运行良好,并且实体被存储/加载到数据库中。我们/我们可以在Spring / Hibernate事务管理中使用私有构造函数/静态工厂方法吗?



我们使用Hibernate注释来映射实体/关系。 applicationContext.xml中没有为与问题相关的域类声明bean定义。这是一个应该有一个静态工厂方法和一个私有构造函数的pojo。



我们如何制作Hibernate(我猜,org.springframework.spring-orm.hibernate3类)使用静态工厂方法而不是构造函数?如果有必要,可能会调用一个私有构造函数?

使用spring工厂方法配置是有道理的,但实体在applicationContext.xml中没有映射为bean。它们只用Hibernate持久化的@Entity注解进行注释。



希望这个编辑能够让问题更清楚(而不是神秘)。 :)

解决方案

虽然我没有使用过Spring,但我在一个项目中使用了Hibernate,该项目必须实例化通过工厂方法或通过多个参数构造函数。

您可以通过Interceptor来完成此任务,Interceptor是一个监听几个关键hibernate事件的类,例如当一个对象需要被实例化或者当一个对象被加载的时候。



为了让Hibernate使用你自己的实例化对象的方法,可以这样做:

  public class MyInterceptor extends EmptyInterceptor {

public Object instantiate(String entityName,EntityMode entityMode,Serializable id){
if (entityName.equals(Foo.class.getName())
return Foo.create();
return null;
}
}

我有点惊讶,你有没有用Hibernate实例化对象的问题在可见的构造函数中,考虑到它可以用反射来解决,而且我的项目中没有这个问题(非类实际上有可见的构造函数)。这可能与Spring有关。检查您使用的是哪个版本的hibernate。


We have a Hibernate/Spring application that have the following Spring beans:

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" />
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" />

When wiring the application together we get the following error when using private constructors in our hibernate entities:

Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: No visible constructors in class 'ourclass'

The entities are typical domain objects such as an Employee or the like.

When changing the constructor's visibility modifier to package (or public) the application runs fine and the entities gets stored/loaded in the database. How do we/can we use private constructors/static factory methods with Spring/Hibernate transaction management?

We use Hibernate annotations to map the entities/relationships. No bean definitions are declared in the applicationContext.xml for the domain class that is related to the problem. It is a pojo that should have a static factory method and a private constructor.

How can we make Hibernate (org.springframework.spring-orm.hibernate3 classes i guess) make use of the static factory method instead of the constructor? Or possibly make it call a private constructor if necessary?

Using the spring factory-method configuration would make sense but the entities are not mapped as beans in our applicationContext.xml. They are only annotated with the @Entity annotation for Hibernate persistence.

Hope this edit clearifies (rather than mystifies) the question. :)

解决方案

While I haven't used Spring, I have used Hibernate in a project which has classes which must either be instantiated by factory methods or through multiple argument constructors.

You can do this through an Interceptor, which is a class which listens in to several key hibernate events, such as when an object needs to be instantiated or when an object is loaded.

To make Hibernate use your own means of instantiating the object, do something like this:

public class MyInterceptor extends EmptyInterceptor {

    public Object instantiate(String entityName, EntityMode entityMode, Serializable id) {
        if(entityName.equals(Foo.class.getName())
             return Foo.create();
        return null;
    }
}

I'm a little suprised that you are having issues with Hibernate not instantiating objects with a non visible constructor, considering that it can be worked around with reflection and I haven't had that issue in my project (non of the classes actually have visible constructors). It may be something with Spring. Check which version of hibernate you are using as well.

这篇关于Hibernate和Spring事务 - 使用私有构造函数/静态工厂方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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