java Hibernate 4.0 中的 HibernateDaoSupport

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11188633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:08:58  来源:igfitidea点击:

HibernateDaoSupport in hibernate 4.0

javaspringhibernatejakarta-ee

提问by samira

i am new in integration of jsf 2.0 spring 3.1 and hibernate 4.1. how can i change following code, because hibernate 4.0 does not include HibernateDaoSupport.

我是 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");

        }
    }

i am trying this sample: http://www.mkyong.com/jsf2/jsf-2-0-spring-hibernate-integration-example/

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

回答by samira

i found the solution. i should use session factory instead.

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

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;

    }
}

回答by Hunter Zhao

Another way to get hibernate session via annotation like following

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

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

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

SessionFactory bean in spring applicationContext.xml

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>

回答by paulsm4

As Samira said above, substituting "SessionFactory" for "HibernateDaoSupport" is the "correct approach" for any new Spring/Hibernate code:

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

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

NOTE: Hibernate access code can also be coded in plain Hibernate style. Hence, for newly started projects, consider adopting the standard Hibernate style of coding data access objects instead, based on SessionFactory.getCurrentSession(). This HibernateTemplate primarily exists as a migration helper for Hibernate 3 based data access code, to benefit from bug fixes in Hibernate 4.x.

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

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

HOWEVER ... I also ran against the same problem in one of the Mkyong.com tutorials:

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

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

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

I'm using Spring 4.2.4.RELEASE and Hibernate 4.3.8.Final.

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

The expedient solution for me (to get the tutorial up/running) is to use Spring-orm's built-in support for HibernateDaoSupport. Specifically, I just changed the line with the import from "hibernate3" to "hibernate4":

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

StockDaoImpl.java =>

StockDaoImpl.java =>

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

In case anybody runs into the same problem :)

万一有人遇到同样的问题:)

回答by Keyur

You can use Hibernate DAO Support by extending HibernateDAOSupport class and overriding its afterPropertiesSet() method.

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

This method is called in HibernateDAO support and at that time since sessionFactory is null it is throwing this error. In your custom class you can set this property explicitly and then call the same method of Parent Class (i.e. HibernateDAOSupport's addProperties() method)

此方法在 HibernateDAO 支持中被调用,当时由于 sessionFactory 为空,因此抛出此错误。在您的自定义类中,您可以显式设置此属性,然后调用父类的相同方法(即 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();
    }
}

Following can be used for sample!

以下可用于示例