Getting "Named query not found" using Spring framework

I have a Java class (Entity) with a set of named queries. When Spring tries to insert a bound bean, it does not find one of the queries.

As an 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 introduce a bean, I get:

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 am sure that the queries are correct (all unit tests pass for them).

Does anyone know a reason for this?

+5
source share
2 answers

Ok, I have a mistake. The following happened:

There was one method in my class annotated with @Resource that called a named query declared in another class, annotated with @Entity).

, Spring , . , "" , , .

, , Spring, .. org.springframework.context.ApplicationListener, onApplicationEvent org.springframework.context.event.ContextRefreshedEvent.

. .

+1
  • , /. @Entity, persistence.xml .

  • - MyEntity.Query1, MyEntity.Query1 ..

  • , , .. .

+5

All Articles