获取“未找到命名查询”使用 Spring 框架

发布于 2024-09-07 22:10:04 字数 727 浏览 0 评论 0原文

我有一个带有一组命名查询的 Java 类(实体)。当 Spring 尝试注入相关 bean 时,它没有找到查询之一。

例如:

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

当 Spring 尝试注入 bean 时,我得到:

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 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无法言说的痛 2024-09-14 22:10:04
  • 确保您的实体已被映射/扫描。它是否用 @Entity 注释,是否添加到 persistence.xml 或相关提供程序配置中,或者是否自动扫描。

  • 我会将类的名称作为查询的前缀 - 即 MyEntity.Query1MyEntity.Query1 等。

  • 验证是否没有部署错误 - 即您的查询是否正确< /p>

  • 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

浮光之海 2024-09-14 22:10:04

嗯,我有错误。发生的情况如下:

在我的类中,有一个用 @Resource 注释的方法,它调用在另一个用 @Entity 注释的类中声明的命名查询。

因此,当 Spring 注入并运行该方法时,它会尝试使用命名查询。但是,该查询尚未“准备好”使用,并且引发的异常是未找到该查询。

为了解决这个问题,我必须运行一个在Spring注入完成时调用的不同方法,即我的类必须实现接口org.springframework.context.ApplicationListener,并且方法onApplicationEvent等待org.springframework.context.event。 ContextRefreshedEvent 事件。

这就是大家。谢谢博佐的帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文