I want to execute a static method in the @NamedQuery of a class. Like this:

@NamedQuery(name = "allFailures", query = "select failureInfo from FailureInfo failureInfo where failureInfo.counter <="+ CountClass.myMethod() + "AND failureInfo.counter <> 0 order by failureInfo.counter asc LIMIT 1")

As you can see, i wanna call the "CountClass.myMethod()"-Method. But I only can choose from variables. My method doesn't show up as an option. Is there a way to call my method in the @NamedQuery?

The Method is static and I can call it within the class. But as I said, not in the @NamedQuery.

有帮助吗?

解决方案

If I understand your issue correctly, you want to parametrize the query. You can use regular parameters with named queries like this:

@NamedQuery(name = "allFailures", query = "select failureInfo from FailureInfo
                                  failureInfo where failureInfo.counter <= :counter 
                                  AND failureInfo.counter <> 0 
                                  order by failureInfo.counter asc LIMIT 1")

Once you have constructed the query, set the parameter, using a static method a regular method or a variable:

TypedQuery<FailureInfo> query =
      em.createNamedQuery("FailureInfo.allFailures", FailureInfo.class);
query.setParameter("counter", MyClass.myMethod());
query.getResultList();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top