【问题标题】:Correct way to use sessionFactorysessionFactory的正确使用方法
【发布时间】:2012-04-26 01:15:08
【问题描述】:

我正在尝试学习 spring 3 和 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);
}

这是正确的还是我在某处犯了错误,因为我无法让它工作? 谢谢

【问题讨论】:

  • 如果您使用的是 Hibernate 3,则不需要使用 HibernateTemplate

标签: java spring hibernate tomcat spring-mvc


【解决方案1】:

这里解释了为什么我们在 Hibernate 3 中不需要任何模板 http://blog.springsource.com/2007/06/26/so-should-you-still-use-springs-hibernatetemplate-andor-jpatemplate/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-17
    • 2011-07-14
    • 1970-01-01
    • 1970-01-01
    • 2015-12-17
    • 2012-02-23
    • 2013-01-09
    • 2010-12-28
    相关资源
    最近更新 更多