Hibernate 4 - DAO call and sessionFactory bean initialization

I am in the middle of upgrading my old program to Spring 3.2 and Hibernate 4 and I ran into several difficulties with sessionFactory (I used to use hibernateTemplate before).

  • I don't think the way to access the DAO is the best way to do it, but I don't see how else to get it to work. If I do a simple DAO object creation (CSSDAO d = new CSSDAOImpl ();) the sessionFactory is always null. If I have what I am doing below it works. What is the proper way to call DAO methods? (please ignore the MVC part of the controller, I know it needs its own work)

  • I am opening a new session in every method in DAO. I know this is not correct as I have to get the current session. But every time I try to get the current session, it says it doesn't exist. How is the session "initialized" the first time? I thought it would inject it based on the XML config, but it doesn't seem to do anything. Any thoughts?

hibernate-cfg.xml

    <?xml version="1.0" encoding="UTF-8"?>


<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

    <bean id="myDataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver"></property>
        <property name="url" value="jdbc:derby:C:\Users\Steven\MyDB"></property>
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="packagesToScan" value="net.form" />
        <property name="dataSource" ref="myDataSource"></property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.DerbyTenSevenDialect
                </prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
        <property name="annotatedClasses">
            <list>
                <value>net.form.StyleChooser</value>
            </list>
        </property>
    </bean>

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

    <bean id="CSSDAO" class="dao.CSSDAOImpl">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

</beans>

      

DAO:

package dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import net.form.StyleChooser;

public class CSSDAOImpl implements CSSDAO {

    private SessionFactory sessionFactory;

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

    @Transactional
    public List selectAllCSS() {
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        List l = session.createCriteria(StyleChooser.class).list();
        session.flush();
        tx.commit();
        return l;
    }

    @Transactional
    public StyleChooser selectCSSById(Integer ID) {
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        StyleChooser sc = (StyleChooser) session.get(StyleChooser.class, ID);
        session.flush();
        tx.commit();        
        return sc;
    }

    @Transactional
    public Integer insertCSS(StyleChooser insertCSS) {
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        Integer id = (Integer) session.save(insertCSS);
        session.flush();
        tx.commit();
        return id;
    }

    @Transactional
    public void deleteCSS(Integer CSSId) {
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        StyleChooser sc = (StyleChooser) session.get(StyleChooser.class, CSSId);
        session.delete(sc);
        session.flush();
        tx.commit();
    }

    @Transactional
    public void updateCSS(StyleChooser cssWithNewValues) {
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        session.update(cssWithNewValues);
        session.flush();
        tx.commit();        
    }
}

      

Accessing the DAO ...

package net.controllers;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import net.form.StyleChooser;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import dao.CSSDAOImpl;

@Controller
@RequestMapping("/stylechoosertable.html")
public class IndexController extends MultiActionController {

    Resource resource = new FileSystemResource(
            "C:/Users/Steven/Desktop/Programming/workspace/CSSGeneratorHibernate4/WebContent/WEB-INF/hibernate.cfg.xml");
    BeanFactory beanFactory = new XmlBeanFactory(resource);
    CSSDAOImpl dao = (CSSDAOImpl) beanFactory.getBean("CSSDAO");

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView showIndex(HttpServletRequest request) throws Exception {
        List<StyleChooser> styleChooser = dao.selectAllCSS();
        return new ModelAndView("stylechoosertable", "styleChooser", styleChooser);
    }
}

      

+1


source to share


1 answer


A few observations:

  • Your getter methods must not use a transaction, that is, they must be non-transactional.

  • Add <tx:annotation-driven transaction-manager="transactionManager"/>

    to your config to find out transactional annotations.

  • If you are using annotation @Transactional

    then you do not need to use programmatic transactions. Add spread attribute to annotation @Transactional

    as @Transactional(propagation=Propagation.REQUIRED)

    and leave transaction control in Hibernate.

  • The first time you need to open a session, if not close, you can use the same session next time. For getting session

    better use the utility method as shown below:

    private Session getSession(SessionFactory sessionFactory){
     Session session = null;
     try{
         session = sessionFactory.getCurrentSession();
     }catch(HibernateException hex){
         hex.printStackTrace();
     }
     if(session == null && !session.isClosed()){
         session = sessionFactory.openSession();
     }
    }
    
          



This way you get the session if available and open a new session otherwise.

+3


source







All Articles