Spring 3注入Hibernate会话的最佳方法3 [英] Best Way to Inject Hibernate Session by Spring 3

查看:91
本文介绍了Spring 3注入Hibernate会话的最佳方法3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定使用Spring3将Hibernate的会话实例注入DAO类的最佳方式是什么。我没有使用Spring的Hibernate模板支持,所以这里是我在DAO类中的代码。

  public void setSessionFactory SessionFactory sessionFactory){
this.sessionFactory = sessionFactory;


$ b $ public SessionFactory getSessionFactory(){
log.info(返回参考会话实例);
if(sessionFactory == null){
log.error(无法找到任何关联的会话);
抛出新的RuntimeException(无法找到任何关联的会话);
}
return sessionFactory;

$ / code>

以下是将会话注入此方法的代码

 < bean id =genericSessionFactoryclass =HibernateSessionFactory
factory-method =getSessionfactoryscope =prototype />

我不确定这是否是执行SessionFactory注入的最佳方法,因为我们不想使用Spring Template作为我们的项目
因此,任何其他的改进建议都会非常有用。

解决方案

Spring Reference暗示了这种用法

  public class ProductDaoImpl implements ProductDao {
$ b $ private SessionFactory sessionFactory;

public void setSessionFactory(SessionFactory sessionFactory){
this.sessionFactory = sessionFactory;
}

公共集合loadProductsByCategory(String category){
return this.sessionFactory.getCurrentSession()
.createQuery(
from test.Product product where product。 category =?)
.setParameter(0,category)
.list();






$ b

这样你的类就没有任何依赖关系春天,你只需要使用普通的Hibernate。


I am not sure whats the best way to inject Hibernate's session instance to DAO classes using Spring3. I am not using Spring's Hibernate Template support for this so here is the code i have in the DAO class.

public void setSessionFactory(SessionFactory sessionFactory){
    this.sessionFactory=sessionFactory;
}


public SessionFactory getSessionFactory(){
    log.info("Returning a refrence to the session instance");
    if(sessionFactory==null){
         log.error("Not able to find any associated session");
         throw new RuntimeException("Not able to find any associated session");
    }
    return sessionFactory;
}

Below is the code for injecting session in to this method

<bean id="genericSessionFactory" class="HibernateSessionFactory"
        factory-method="getSessionfactory" scope="prototype/>

I am not sure if this is the best way to do SessionFactory injection since we don't want to use Spring Template for our project. So any other suggestion for improvement will be much helpfull.

解决方案

The Spring Reference suggests this usage:

public class ProductDaoImpl implements ProductDao {

    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public Collection loadProductsByCategory(String category) {
        return this.sessionFactory.getCurrentSession()
                .createQuery(
                    "from test.Product product where product.category=?")
                .setParameter(0, category)
                .list();
    }
}

That way your classes don't have any dependencies to Spring, you just use plain Hibernate.

这篇关于Spring 3注入Hibernate会话的最佳方法3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆