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!

有帮助吗?

解决方案

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.

其他提示

I think you are using HQL if yes then there is a mistake in the syntax.use as below, query = "select p from Person p".

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top