【问题标题】:HibernateDaoSupport in hibernate 4.0休眠 4.0 中的 HibernateDaoSupport
【发布时间】:2012-06-26 16:00:36
【问题描述】:

我是 jsf 2.0 spring 3.1 和 hibernate 4.1 集成的新手。我如何更改以下代码,因为 hibernate 4.0 不包含 HibernateDaoSupport。

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;


    public class CustomerDaoImpl extends 
           HibernateDaoSupport implements CustomerDao{

        public void addCustomer(Customer customer){

            customer.setCreatedDate(new Date());
            getHibernateTemplate().save(customer);

        }

        public List<Customer> findAllCustomer(){

            return getHibernateTemplate().find("from Customer");

        }
    }

我正在尝试这个示例:http://www.mkyong.com/jsf2/jsf-2-0-spring-hibernate-integration-example/

【问题讨论】:

标签: java spring hibernate jakarta-ee


【解决方案1】:

我找到了解决方案。我应该改用会话工厂。

import java.util.List;

import org.hibernate.SessionFactory;

public class CustomerDaoImpl implements CustomerDao{
    private SessionFactory sessionFactory;
    public SessionFactory getSessionFactory() {
        return sessionFactory;}
    public void setSessionFactory(SessionFactory sessionFactory) {
         this.sessionFactory = sessionFactory;
    }

    public void addCustomer(Customer customer){


        getSessionFactory().getCurrentSession().save(customer);

    }

    public List<Customer> findAllCustomer(){

        List list = getSessionFactory().getCurrentSession().createQuery("from Customer").list();
        return list;

    }
}

【讨论】:

  • 对我来说,这段代码遇到了错误class org.hibernate.HibernateException save is not valid without active transaction 所以我必须扩展addCustomer 并最终得到Transaction trans = getSessionFactory().getCurrentSession().beginTransaction(); getSessionFactory().getCurrentSession().save(p); trans.commit();
  • 如果我的查询位于 xml 文件中怎么办?有没有 findByNamedQuery 的替代品?
【解决方案2】:

另一种通过如下注释获取休眠会话的方法

@Autowired
@Qualifier("sessionFactory")
private SessionFactory sessionFactory;

public Session getSession() {
    return sessionFactory.getCurrentSession();
}

spring applicationContext.xml中的SessionFactory bean

    <!--  sessionFactory -->
    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="packagesToScan">
            <list>
                <value></value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props></props>
        </property>
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
    </bean>

【讨论】:

    【解决方案3】:

    您可以通过扩展 HibernateDAOSupport 类并覆盖其 afterPropertiesSet() 方法来使用 Hibernate DAO 支持。

    在 HibernateDAO 支持中调用此方法,当时由于 sessionFactory 为 null,因此引发此错误。在您的自定义类中,您可以显式设置此属性,然后调用父类的相同方法(即 HibernateDAOSupport 的 addProperties() 方法)

    package com.techcielo.spring4.hibernate.template;
    
    import org.hibernate.SessionFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.orm.hibernate3.HibernateTemplate;
    import org.springframework.stereotype.Component;
    
    @Component("hibernateTemplate")
    public class Hibernate4CustomTemplate extends HibernateTemplate{
    
        @Autowired(required=true)
        private SessionFactory sessionFactory;
        public void setSessionFactory(SessionFactory sessionFactory) {
            System.out.println("Setting SessionFactory");
            this.sessionFactory = sessionFactory;
            super.setSessionFactory(sessionFactory);
        }
    
        @Override
        public void afterPropertiesSet() {
        System.out.println("Checking if properties set..."+this.sessionFactory);
        setSessionFactory(sessionFactory);
        super.afterPropertiesSet();
        }
    }
    

    以下可用于sample

    【讨论】:

    • 请注意,link-only answers are discouraged,SO 答案应该是搜索解决方案的终点(相对于另一个参考中途停留,随着时间的推移往往会变得陈旧)。请考虑在此处添加独立的概要,并保留链接作为参考。
    【解决方案4】:

    正如 Samira 上面所说,用“SessionFactory”代替“HibernateDaoSupport”是任何新的 Spring/Hibernate 代码的“正确方法”:

    https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/orm/hibernate4/support/HibernateDaoSupport.html

    注意:Hibernate 访问代码也可以用纯 Hibernate 编码 风格。因此,对于新启动的项目,可以考虑采用 代替编码数据访问对象的标准 Hibernate 风格,基于 在 SessionFactory.getCurrentSession() 上。这个休眠模板 主要作为基于 Hibernate 3 的数据的迁移助手存在 访问代码,以受益于 Hibernate 4.x 中的错误修复。

    但是......我在 Mkyong.com 的一个教程中也遇到了同样的问题:

    http://www.mkyong.com/spring/maven-spring-hibernate-mysql-example/

    我正在使用 Spring 4.2.4.RELEASE 和 Hibernate 4.3.8.Final。

    对我来说(让教程启动/运行)的权宜之计是使用 Spring-orm 对 HibernateDaoSupport 的内置支持。具体来说,我只是将导入的行从“hibernate3”更改为“hibernate4”:

    StockDaoImpl.java =>

    package com.mkyong.stock.dao.impl;
    ...
    // import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
    import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
    ...
    

    如果有人遇到同样的问题:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-09
      • 2012-01-26
      • 2011-12-16
      • 2015-04-28
      • 2018-02-06
      相关资源
      最近更新 更多