Search code examples
hibernatenamed-query

Hibernate Named Query is Null


I have a problem with the hibernate named queries.

My Entity:

import org.hibernate.annotations.NamedQueries;
import org.hibernate.annotations.NamedQuery;

import javax.persistence.Entity;

@NamedQueries({ @NamedQuery(name = "getAllPersons", query = "select * from person p") })
@Entity
public class Person extends Party { .... }

My DAO:

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

@Repository
public class DefaultPersonDao implements PersonDao{

    @Override
    @SuppressWarnings("unchecked")
    public List<Person> getAllPersons() {
        Query query = getSession().getNamedQuery("getAllPersons");
        List<Person> persons = query.list();
        return persons;
    }
}

I always get a NullPointerException when I do query.list(), because the query I get from the sessionfactory is null.

(getSession() returns a import org.hibernate.SessionFactory)

Does anyone see the mistake?

Thanks!


Solution

  • Check the logs, you must have an error logged somewhere, because the query is invalid. select * is a SQL query, not a JPQL (or HQL) one. It should be select p from Person p.