HibernateDaoSupport in sleep mode 4.0

I am new to integrating jsf 2.0 spring 3.1 and hibernate 4.1. how can I change the 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 try this example: http://www.mkyong.com/jsf2/jsf-2-0-spring-hibernate-integration-example/

+5
source share
4 answers

I found a solution. Instead, I should use a factory session.

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;

    }
}
+11
source

Another way to get a hibernation session is via annotation as shown below

@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>
+3
source

, "SessionFactory" "HibernateDaoSupport" - " " Spring/Hibernate:

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

. Hibernate . , Hibernate SessionFactory.getCurrentSession(). HibernateTemplate 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;
...

- :)

+1

Hibernate DAO Support, HibernateDAOSupport afterPropertiesSet().

This method is called in support of HibernateDAO, and while sessionFactory is null, it causes this error. In your custom class, you can explicitly set this property and then call the same method of the parent class (i.e. the HibernateDAOSupport addProperties () method)

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();
    }
}

Below is an example sample !

-2
source

All Articles