2012-04-13 77 views
3

我试图学习春天3和DAO和BO类以及如何自动装配它,我想知道这是正确的方式来连接sessionFactory,因为我已经读过,它是更好使用正确的方法来使用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); 
} 

这是正确的还是我犯了一个错误的地方,因为我不能得到它的工作? 感谢

+1

如果您在使用Hibernate 3,您不需要使用的HibernateTemplate – vinodn 2012-04-13 16:44:48

回答