当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Execute static method in @NamedQuery-head

问题描述:

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();
分享给朋友:
您可能感兴趣的文章:
随机阅读: