Alexandr
Alexandr

Reputation: 9515

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)

Upvotes: 0

Views: 2534

Answers (1)

stacker
stacker

Reputation: 69002

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.

Upvotes: 1

Related Questions