Question

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?

Was it helpful?

Solution 2

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.

OTHER TIPS

  • 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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top