在 JPQL 命名查询中指定注释

Specify comments in JPQL named queries

我们有一些场景,其中 2 个应用程序指向同一个 oracle database/tables 并执行一些查询。 DB 团队建议在 SQL 中使用评论,帮助他们确定应该联系谁来处理缓慢的 运行 查询。

由于我们使用的是 JPA,因此我不确定 我们如何为 JPA 转换为本机 SQL 的 JPQL 查询定义注释。 如果我们在 JPQL 中指定注释,那么它会中断

        @NamedQuery(name = Consts.LOOKUP_BY_END_DATE, query = "
/* comment */ SELECT b FROM TableB b WHERE "
                + " b.id.ObjId=:ObjId AND b.id.persId=:persId and b.recEffEndDayKy=:recEffEndDayKy "),

异常:

Exception Description: Syntax error parsing [/* comments */SELECT b FROM TableB b WHERE  b.id.ObjId=:ObjId AND b.id.PersId=:PersId and b.recEffEndDayKy=:recEffEndDayKy ]. 
[0, 157] The query does not start with a valid identifier, has to be either SELECT, UPDATE or DELETE FROM.
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildException(HermesParser.java:155)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.validate(HermesParser.java:334)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.populateQueryImp(HermesParser.java:278)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildQuery(HermesParser.java:163)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:142)
    at org.eclipse.persistence.internal.jpa.JPAQuery.processJPQLQuery(JPAQuery.java:223)
    at org.eclipse.persistence.internal.jpa.JPAQuery.prepare(JPAQuery.java:184)
    at org.eclipse.persistence.queries.DatabaseQuery.prepareInternal(DatabaseQuery.java:624)
    at org.eclipse.persistence.internal.sessions.AbstractSession.processJPAQuery(AbstractSession.java:4366)
    at org.eclipse.persistence.internal.sessions.AbstractSession.processJPAQueries(AbstractSession.java:4326)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.initializeDescriptors(DatabaseSessionImpl.java:598)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.postConnectDatasource(DatabaseSessionImpl.java:818)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:762)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:265)
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:731)
    ... 33 more

我相信您将接受 SQL 的本机查询与命名 JPQL 查询混淆了。 JPQL 不支持注释。如果要控制查询,则必须使用 SQL,或使用本机 EclipseLink 功能。请参阅此处描述的 'SQL' 函数 http://java-persistence-performance.blogspot.com/2012/05/jpql-vs-sql-have-both-with-eclipselink.html