Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting 'Named query not found' with Spring framework

I have a Java class (Entity) with a set of named queries. When the Spring tries to inject the related bean, it is not finding one of the queries.

As example:

@NamedQueries({
        @NamedQuery(name = "Query1", query = "..."),
        @NamedQuery(name = "Query2", query = "..."),
        @NamedQuery(name = "Query3", query = "..."),
        @NamedQuery(name = "Query4", query = "..."),
        @NamedQuery(name = "Query5", query = "...")
})

When Spring tries to inject the bean, I´m getting:

org.springframework.beans.factory.BeanCreationException: Error creating bean
with name 'myBean': Injection of resource methods failed;nested exception is
java.lang.IllegalArgumentException: Named query not found: Query3 at ...

I´m sure the queries are correct (all the unit tests for them are passing).

Does anybody know the root cause for it?

like image 663
Felipe Arenales Avatar asked Jun 30 '10 23:06

Felipe Arenales


People also ask

How do I create a named query in spring boot?

We can use @NamedQuery annotation to specify a named query within an entity class and then declare that method in repository. Following is an example. We've added custom methods in Repository in JPA Custom Methods chapter. Now let's add another method using @NamedQuery and test it.

What is JPA named query?

A named query is a predefined query that you create and associate with a container-managed entity (see "Using Annotations"). At deployment time, OC4J stores named queries on the EntityManager . At run time, you can use the EntityManager to acquire, configure, and execute a named query.

What is named query annotation?

Annotation Type NamedQuery. Specifies a static, named query in the Java Persistence query language. Query names are scoped to the persistence unit. The NamedQuery annotation can be applied to an entity or mapped superclass.

What is named query?

A named query is a SQL expression represented as a table. In a named query, you can specify an SQL expression to select rows and columns returned from one or more tables in one or more data sources.


2 Answers

  • make sure your entity has been mapped / scanned. Is it annotated with @Entity, is it added to the persistence.xml or to the relevant provider configuration, or is it automatically scanned.

  • I'd prefix the name of the class to the query - i.e. MyEntity.Query1, MyEntity.Query1 etc.

  • verify whether there aren't deployment errors - i.e. that your query is correct

like image 123
Bozho Avatar answered Oct 02 '22 13:10

Bozho


Well, I´ve got the error. What was happening is as follows:

In my class there was one method annotated with @Resource, which called the named query declared in another class annotated with @Entity).

So, when Spring injects and runs the method, it tries to use the named query. However, the query is not 'ready' to be used, and the exception throwed is that the query was not found.

To solve this, I have to run a different method called when the Spring injections are finished, i.e., my class has to implement the interface org.springframework.context.ApplicationListener and the method onApplicationEvent waits for a org.springframework.context.event.ContextRefreshedEvent event.

That´s all guys. Thank you Bozho for your help.

like image 37
Felipe Arenales Avatar answered Oct 02 '22 13:10

Felipe Arenales