Spring 3 Annotations - HibernateDaoSupport - Repository Requires Session Factory
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3230608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring 3 Annotations - HibernateDaoSupport - Repository Requires Session Factory
提问by walnutmon
I am getting an exception saying :
我收到一个异常说:
java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required
java.lang.IllegalArgumentException: 'sessionFactory' 或 'hibernateTemplate' 是必需的
When trying to use the @Repositoryannotation on a HibernateDaoSupport class. The error message is straightforward, in order to create the Repository it needs a sessionFactory. However,I have defined a session factory in my XML:
尝试@Repository在 HibernateDaoSupport 类上使用注释时。错误消息很简单,为了创建存储库,它需要一个 sessionFactory。但是,我在我的 XML 中定义了一个会话工厂:
<!-- Hibernate -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dashDataSource" />
<property name="annotatedClasses">
<list>
<value>com.mycomp.myapp.Category</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
</bean>
So I'm not sure how to give the repository the SessionFactory that it requires while it's creating it's annotation driven beans, I attempted to do the following:
因此,我不确定如何为存储库提供它在创建注释驱动 bean 时所需的 SessionFactory,我尝试执行以下操作:
@Autowired
protected HibernateTemplate createHibernateTemplate(SessionFactory sessionFactory) {
return super.createHibernateTemplate(sessionFactory);
}
But this does not solve the problem, likely because the repository needs that property while instantiating, not just when performing an action. Unfortunately, I don't know how to get around this problem because there are no constructors or initialization methods to override with a @Autowired annotation.
但这并不能解决问题,可能是因为存储库在实例化时需要该属性,而不仅仅是在执行操作时。不幸的是,我不知道如何解决这个问题,因为没有使用 @Autowired 注释覆盖的构造函数或初始化方法。
I checked to make sure the sessionFactory bean is being created and can be Autowired, and that is fine.
我检查以确保正在创建 sessionFactory bean 并且可以自动装配,这很好。
回答by axtavt
HibernateDaoSupportis supplied with SessionFactoryvia setSessionFactory(). However, setSessionFactory()is final, so you can't override it to add an @Autowiredannotation. But you can apply @Autowiredto the arbitrary method and call setSessionFactory()from it:
HibernateDaoSupport提供了SessionFactoryvia setSessionFactory()。但是,setSessionFactory()is final,因此您无法覆盖它以添加@Autowired注释。但是您可以应用@Autowired到任意方法并setSessionFactory()从中调用:
@Autowired
public void init(SessionFactory factory) {
setSessionFactory(factory);
}
回答by atrain
You can also define which session factory you want to use (if you have more than one, for instance), by using the @Qualifierannotation:
您还可以使用@Qualifier注释定义要使用的会话工厂(例如,如果您有多个会话工厂):
@Autowired
public MyClassImpl(@Qualifier("myOtherSessionFactory") SessionFactory sessionFactory) {
setSessionFactory(sessionFactory);
}
You can also toggle between data sources by extending the AbstractRoutingDataSource. See this page.
您还可以通过扩展AbstractRoutingDataSource. 请参阅此页面。

