Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HibernateDaoSupport in hibernate 4.0

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.

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/

like image 849
samira Avatar asked Jun 25 '12 11:06

samira


3 Answers

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;

    }
}
like image 161
samira Avatar answered Sep 27 '22 23:09

samira


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

    <!--  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>
like image 34
Hunter Zhao Avatar answered Sep 27 '22 22:09

Hunter Zhao


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

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.

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

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

I'm using Spring 4.2.4.RELEASE and 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":

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 :)

like image 22
paulsm4 Avatar answered Sep 27 '22 23:09

paulsm4