Hibernate SessionFactory 与 Spring LocalSessionFactoryBean
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18011759/
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
Hibernate SessionFactory vs Spring LocalSessionFactoryBean
提问by IUnknown
I am trying to move a spring3.2.x + hibernate4.x setup from a xml to java configuration.
Heres a snippet of the existing code:
我正在尝试将 spring3.2.x + hibernate4.x 设置从 xml 移动到 java 配置。
这是现有代码的片段:
import org.hibernate.SessionFactory;
import org.hibernate.Query;
import org.hibernate.Session;
public class StockDaoImpl implements StockDao{
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public void save(Stock stock){
Session session = getSessionFactory().openSession();
try{
session.save(stock);
}
finally{
session.close();
}
}
The spring config file
弹簧配置文件
<!-- Stock Data Access Object -->
<bean id="stockDao" class="com.data.dao.StockDaoImpl" >
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="dataSource">
.....
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="hibernateProperties">
......
</property>
<property name="mappingResources">
.......
</property>
</bean>
This works fine - but how do i re-define this config in java?
Heres an attempt -
这工作正常 - 但我如何在 java 中重新定义这个配置?
这是一个尝试——
@Bean
public StockDao stockDao() {
StockDaoImpl dao=new StockDaoImpl();
dao.setSessionFactory(sessionFactory());
return dao;
}
......
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(restDataSource());
sessionFactory.setPackagesToScan(new String[] { .....});
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
But this wont compile as the sessionFactorybeing referred to is not Spring's LocalSessionFactoryBean?
但这不会编译,sessionFactory因为所指的不是 Spring 的 LocalSessionFactoryBean?
How do I reconcile this?
我该如何调和这一点?
回答by JB Nizet
LocalSessionFactoryBean is a FactoryBean<SessionFactory>. It means that it allows creating instances of SessionFactory.
LocalSessionFactoryBean 是一个FactoryBean<SessionFactory>. 这意味着它允许创建 SessionFactory 的实例。
So you just need
所以你只需要
public StockDao stockDao() {
StockDaoImpl dao=new StockDaoImpl();
dao.setSessionFactory(sessionFactory().getObject());
return dao;
}
But I wouldn't create DAOs explicitel like that. Simply annotate your DAO with @Repository, and autowire the session factory using @Autowired. Make sure the Java config class is annotated with @ComponentScan("the.base.package.of.daos")
但是我不会像这样创建 DAO 的显式。只需使用@Repository 注释您的DAO,并使用@Autowired 自动装配会话工厂。确保 Java 配置类用@ComponentScan("the.base.package.of.daos")
回答by Luca Basso Ricci
Missing getObject() call in return.
Substituite with return sessionFactory.getObject()(and change return type,too!)
缺少 getObject() 调用作为回报。
替换为return sessionFactory.getObject()(并更改返回类型!)

