Hibernate 命名查询为空
我的休眠命名查询有问题。
我的实体:
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 { .... }
我的 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;
}
}
当我执行 query.list()
时,我总是收到 NullPointerException,因为我从 sessionfactory 获取的查询为 null。
(getSession()
返回一个import org.hibernate.SessionFactory
)
有人看到这个错误吗?
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
检查日志,您一定在某处记录了错误,因为查询无效。
select *
是一种 SQL 查询,而不是 JPQL(或 HQL)查询。应该是从 Person p 中选择 p
。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 beselect p from Person p
.我认为您正在使用 HQL 如果是,那么语法中有错误。使用如下,
query =“从人员 p 中选择 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"
.