2012-04-13 117 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

回答