我们有一个Hibernate/Spring应用程序,它具有以下Spring bean:
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" />
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" />
Run Code Online (Sandbox Code Playgroud)
将应用程序连接在一起时,在我们的hibernate实体中使用私有构造函数时会出现以下错误:
Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: No visible constructors in class 'ourclass'
Run Code Online (Sandbox Code Playgroud)
实体是典型的域对象,例如Employee等.
将构造函数的可见性修饰符更改为包(或公共)时,应用程序运行正常,实体将存储/加载到数据库中.我们如何/可以在Spring/Hibernate事务管理中使用私有构造函数/静态工厂方法?
我们使用Hibernate注释来映射实体/关系.applicationContext.xml中没有为与该问题相关的域类声明bean定义.它是一个pojo,应该有一个静态工厂方法和一个私有构造函数.
我怎样才能使Hibernate(org.springframework.spring-orm.hibernate3类)使用静态工厂方法而不是构造函数?或者可能在必要时调用私有构造函数?
使用spring factory-method配置是有意义的,但实体不会在applicationContext.xml中映射为bean.它们仅使用Hibernate持久性的@Entity注释进行注释.
希望这个编辑能够澄清(而不是神秘)这个问题.:)