Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8185185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:45:33+00:00 2026-06-07T01:45:33+00:00

I’m trying to integrate Spring 3.1 with Hibernate 4.1.4, but I am having problems

  • 0

I’m trying to integrate Spring 3.1 with Hibernate 4.1.4, but I am having problems when getting my session from a generic DAO.

The problem is, if I use getCurrentSession() I get a NullPointerException and Hibernate says No session is bound to the context, yet if I use openSession everything seems to be working. Somehow I feel I should be using getCurrentSession, but can’t find the origin of the problem.

I’ve searched this over the internet but none of the solutions worked for me.

Here’s my genericDAO code:

public class GenericDaoHibernateImpl<E, PK extends Serializable> implements GenericDao<E, PK> {

    private Class<E> entityClass;

    protected GenericDataBaseExceptionHandler exceptionHandler;

    private SessionFactory sessionFactory;  

    @SuppressWarnings("unchecked")
    public GenericDaoHibernateImpl() {
        this.entityClass = (Class<E>) ((ParameterizedType) getClass().
            getGenericSuperclass()).getActualTypeArguments()[0];
    }

    public void setExceptionHandler(GenericDataBaseExceptionHandler exceptionHandler) {
        this.exceptionHandler = exceptionHandler;
    }

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

    protected Session getSession() {
        if (sessionFactory.getCurrentSession() == null)
            throw new IllegalStateException("Session has not been set on DAO before usage");
        return sessionFactory.getCurrentSession(); //This crashes
//      return sessionFactory.openSession(); //This does not
    }

    public Class<E> getEntityClass() {
        return entityClass;
    }

    public void persist(E entity) throws GenericDataBaseException {
        try {
            getSession().persist(entity);       
        } catch (Throwable t) {
            Collection<Object> args = new ArrayList<Object>();
            args.add(entity);
            throw exceptionHandler.handle(t, "persist", args);
        }
    }
    (...)

}

And my spring config file:

    <context:annotation-config />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <property name="driverClass">
            <value>com.mysql.jdbc.Driver</value>
        </property>
        <property name="jdbcUrl">
            <value>jdbc:mysql://localhost/forestool</value>
        </property>
        <property name="user">
            <value>forestool</value>
        </property>
        <property name="password">
            <value>forestool</value>
        </property>
    </bean>


    <!-- Declaración de la factoría de sesiones hibernate con los mappings necesarios -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource"/>
        </property>
        <property name="mappingLocations">
            <list>
                <value>classpath*:/hbm/*.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <ref bean="hibernateProperties"/>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
     <property name="sessionFactory">
         <ref bean="sessionFactory"/>
     </property>
    </bean>
<bean id="hibernateProperties" class="java.util.Properties">
    <constructor-arg index="0">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
        </props>
    </constructor-arg>
</bean>

Exception:

org.hibernate.HibernateException: No Session found for current thread
    at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
    at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1041)
    at es.fsc.core.dao.impl.GenericDaoHibernateImpl.getSession(GenericDaoHibernateImpl.java:83)
    at es.fsc.core.dao.impl.GenericDaoHibernateImpl.findAll(GenericDaoHibernateImpl.java:244)
    at es.fsc.dao.explotacion.impl.ExplotacionDaoImpl.getExplotacionesAbiertas(ExplotacionDaoImpl.java:21)
    at es.fsc.service.explotacion.impl.ExplotacionServiceImpl.getExplotacionesAbiertas(ExplotacionServiceImpl.java:22)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:196)
    at $Proxy5.getExplotacionesAbiertas(Unknown Source)
    at es.fsc.app.FscApp.runApp(FscApp.java:58)
    at es.fsc.app.App.run(App.java:65)
    at es.fsc.main.Main.main(Main.java:20)
Exception in thread "main" java.lang.NullPointerException
    at es.fsc.core.dao.impl.GenericDaoHibernateImpl.findAll(GenericDaoHibernateImpl.java:253)
    at es.fsc.dao.explotacion.impl.ExplotacionDaoImpl.getExplotacionesAbiertas(ExplotacionDaoImpl.java:21)
    at es.fsc.service.explotacion.impl.ExplotacionServiceImpl.getExplotacionesAbiertas(ExplotacionServiceImpl.java:22)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:196)
    at $Proxy5.getExplotacionesAbiertas(Unknown Source)
    at es.fsc.app.FscApp.runApp(FscApp.java:58)
    at es.fsc.app.App.run(App.java:65)
    at es.fsc.main.Main.main(Main.java:20)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T01:45:35+00:00Added an answer on June 7, 2026 at 1:45 am

    I don’t see anything in this configuration that would actually open a Hibernate session for you, or control when that is done.

    If you are new to Spring, you might just want to configure an OpenSessionInViewFilter in your web.xml to have a Session open and bound to each request automatically.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.