使用 if else 语句进行命名查询?

发布于 2024-10-08 20:50:18 字数 413 浏览 0 评论 0原文

@NamedQueries(  
   {   
 @NamedQuery(name = "GetAvailableProducts", query = new StringBuilder("").append("SELECT p   FROM Product p WHERE p.type= :type AND (p.available = 'ALL' OR").append(isTest() ? "(p.available = 'TEST' OR)"  : " ").append("p.available = :available)")),  
 }

这给了我一个错误,它无法识别 isTest() 方法。如果我将 if 语句如 if(1==1) 或类似的东西放在 Intellij IDEA 中,它会显示“属性必须是常量”,而不是这种方法。怎么解决?

@NamedQueries(  
   {   
 @NamedQuery(name = "GetAvailableProducts", query = new StringBuilder("").append("SELECT p   FROM Product p WHERE p.type= :type AND (p.available = 'ALL' OR").append(isTest() ? "(p.available = 'TEST' OR)"  : " ").append("p.available = :available)")),  
 }

This gives me an error it can't recognize the isTest() method. Instead of this method if I put an if statement as lik if(1==1) or something like that, it says "Attribute must be constans" in Intellij IDEA. How to solve?

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

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

发布评论

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

评论(2

醉态萌生 2024-10-15 20:50:18

Java注解的参数只能是编译时常量。这是行不通的。

参考页面: 注释

引用:

定义注释类型后,您可以使用它来注释声明。注释是一种特殊类型的修饰符,可以在可以使用其他修饰符(例如 public、static 或 Final)的任何地方使用。按照惯例,注释位于其他修饰符之前。注释由一个 at 符号 (@) 后跟一个注释类型和一个带括号的元素值对列表组成。 这些值必须是编译时常量。

The parameters of Java annotations can only be compile-time constants. This can't work.

Reference page: Annotations

Quote:

Once an annotation type is defined, you can use it to annotate declarations. An annotation is a special kind of modifier, and can be used anywhere that other modifiers (such as public, static, or final) can be used. By convention, annotations precede other modifiers. Annotations consist of an at-sign (@) followed by an annotation type and a parenthesized list of element-value pairs. The values must be compile-time constants.

独孤求败 2024-10-15 20:50:18

我不相信你可以通过 NamedQuery 来做到这一点。

创建多个命名查询,或使用动态查询:

 Query query = em.createQuery(...); 

I don't believe you can do this from a NamedQuery.

Either create multiple named queries, or use a dynamic query instead:

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