使用 if else 语句进行命名查询?
@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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Java注解的参数只能是编译时常量。这是行不通的。
参考页面: 注释
引用:
The parameters of Java annotations can only be compile-time constants. This can't work.
Reference page: Annotations
Quote:
我不相信你可以通过 NamedQuery 来做到这一点。
创建多个命名查询,或使用动态查询:
I don't believe you can do this from a NamedQuery.
Either create multiple named queries, or use a dynamic query instead: