我正在尝试学习Spring3、DAO和BO类,以及如何使用它进行自动连接,我想知道这是连接sessionFactory的正确方法吗?因为我读到过,最好使用
public void save(Customer customer) {
sessionFactory.getCurrentSession().save(customer);
}而不是
public void save(Customer customer){
getHibernateTemplate().save(customer);
}那么,以下是连接sessionFactory的正确方法吗?
CustomHibernateDaoSupport类
package com.fexco.helloworld.web.util;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public abstract class CustomHibernateDaoSupport extends HibernateDaoSupport
{
@Autowired
@Qualifier("sessionFactory")
public void seSessionFactory(SessionFactory sessionFactory) {
this.setSessionFactory(sessionFactory);
}
}CustomerDaoImpl类
package com.fexco.helloworld.web.dao;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.fexco.helloworld.web.model.Customer;
import com.fexco.helloworld.web.util.CustomHibernateDaoSupport;
@Repository("customerDao")
public class CustomerDaoImpl extends CustomHibernateDaoSupport implements CustomerDao{
@Autowired
private SessionFactory sessionFactory;
public void save(Customer customer) {
sessionFactory.getCurrentSession().save(customer);
}这是正确的,还是我在什么地方犯了错误,因为我不能让它工作?谢谢
发布于 2012-04-14 00:50:49
这里解释了为什么Hibernate 3 http://blog.springsource.com/2007/06/26/so-should-you-still-use-springs-hibernatetemplate-andor-jpatemplate/不需要任何模板
https://stackoverflow.com/questions/10144804
复制相似问题