我们有一些场景,其中2个应用程序指向相同的oracle数据库/表并执行一些查询。 数据库团队建议在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
答案 0 :(得分:2)
我相信你已经混淆了接受带有命名JPQL查询的SQL的本机查询。 JPQL不支持评论。如果要控制查询,则必须使用SQL,或使用本机EclipseLink功能。查看' SQL'此处描述的功能http://java-persistence-performance.blogspot.com/2012/05/jpql-vs-sql-have-both-with-eclipselink.html