用Hibernate无法解析JPA命名查询

时间:2013-05-10 12:35:31

标签: java hibernate jpa-2.0 named-query

我添加了新的命名查询:

@Entity(name = "books")
@NamedQueries({
        @NamedQuery(name = "books.findByCategoryId",
                query = "SELECT DISTINCT b.* FROM books b WHERE b.categoryId =:categoryId")
})
public class Book implements Serializable {

我使用Hibernate DAO实现:

@Override
    public Set<Book> findByCategory(Long categoryId) {
        return  new LinkedHashSet<Book>( sessionFactory.getCurrentSession().
                getNamedQuery("books.findByCategoryId").
                setParameter("categoryId", categoryId).list());
    }

由于IDE中的某些原因,我看到:无法解析查询'books.findByCategoryId'。 当我启动应用程序时,我得到了:

may 10, 2013 6:55:05 PM org.apache.catalina.core.StandardContext loadOnStartup
SEVERE: Servlet /SpringWebFlow threw load() exception
org.hibernate.HibernateException: Errors in named queries: books.findByCategoryId
        at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:528)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1760)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1798)

1 个答案:

答案 0 :(得分:1)

Hibernate错误:“Errors in named queries: books.findByCategoryId”表示您的查询存在语法问题。来自IDE的消息有点误导。正如@Piotr所指出的,在您的查询中将“b。*”更改为“b”。

The Hibernate Query Language未提及通配符。