문제

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

도움이 되었습니까?

해결책

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.

다른 팁

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(...); 
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top