Hibernate, Spring, Dao getSession() NullPointerException

I get the error.

java.lang.NullPointerException
at org.springframework.orm.hibernate3.support.HibernateDaoSupport.getSession(HibernateDaoSupport.java:143)
at com.walladverts.model.dao.UserDao.findByUsername(UserDao.java:25)

while accessing getSession() in findByUsername

package com.walladverts.model.dao;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import com.walladverts.exceptions.DataNotFoundException;
import com.walladverts.model.entities.User;
import com.walladverts.util.CustomHibernateDaoSupport;@
Repository("userDao")
public class UserDao extends CustomHibernateDaoSupport {
    public void save(User user) {
        getHibernateTemplate().save(user);
    }
    public void delete(User user) {
        getHibernateTemplate().delete(user);
    }
    public User findByUsername(String username) throws DataNotFoundException {
        Session session = getSession();
        Criteria crit = session.createCriteria(User.class);
        System.out.println(username);
        crit.add(Restrictions.eq("username", username));
        crit.setMaxResults(1);
        List < User > users = crit.list();
        System.out.println(users);
        if (users.size() < 1) {
            throw new DataNotFoundException();
        }
        return users.get(0);
    }
}

Parent class:

package com.walladverts.util;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public abstract class CustomHibernateDaoSupport extends HibernateDaoSupport {@
    Autowired
    public void init(SessionFactory factory) {
        setSessionFactory(factory);
    }
}

Does anybody has an idea why this occurs? It freezes me for developing.

EDIT:

It happens when Spring Secure tries to sign in an user. Also my SessionFactory is working OK when calling this method from Controller.

Answers


public abstract class CustomHibernateDaoSupport extends HibernateDaoSupport {
    @Autowired
    @Qualifier("sessionFactory")
    private SessionFactory sessionFactory´╝Ť

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

public User findByUsername(String username) throws DataNotFoundException {
     Session session = getSession();
     // do sth
}

Need Your Help

Disabling touchevents on overlaying div so underlying div recieves them

javascript jquery javascript-events

I'm working on a mobile web gallery where users can drag slide images left and right and all works great with that.