简体   繁体   中英

JPA named query cannot be resolved with Hibernate

I have added new 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 {

I use Hibernate DAO implementation:

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

For some reason in IDE I see: Cannot resolve query 'books.findByCategoryId'. When I start the application I'm getting:

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)

The Hibernate error: " Errors in named queries: books.findByCategoryId " means that you have a syntax issue with the query. The message from your IDE is a bit misleading. As pointed out by @Piotr change "b.*" to "b" in your query.

The The Hibernate Query Language doesn't mention a wildcard.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM