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

Building an array of non-sequential variables

arrays sas

I have a dataset with ~6000 variables, which are in 82 groups. The variable names are all in the form "a001yr, a001sta, a001tot... a082tot" (in other words, "acyclenumsetsuffix"). Is there a way to

DocumentDb Spatial Distance in Linq returns weird result

c# linq azure azure-documentdb

When I do a documentdb linq distance query on two Points (Microsoft.Azure.Documents.Spatial) that are the same I get different results.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.