Faisal Ahmed Siddiqui
Faisal Ahmed Siddiqui

Reputation: 707

Specify comments in JPQL named queries

We have some scenario where 2 applications points to the same oracle database/tables and perform some queries. DB Team suggested to use comments in SQL that helps them to identify who should they contact to for slow running queries.

Since we are using the JPA i am not sure how can we define comments for the JPQL queries that are converted into native SQL by JPA. If we specify the comments in JPQL then it breaks

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

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

Upvotes: 1

Views: 3442

Answers (2)

SerhiiH
SerhiiH

Reputation: 11

Hints can be used with @NamedQuery to specify the comments,for example:

@NamedQuery(name="someQuery",
        query="SELECT 1 FROM DUAL",
        hints={@QueryHint(name="org.hibernate.comment", value="very_important_comment")})

Upvotes: 0

Chris
Chris

Reputation: 21145

I believe you have confused native queries which accept SQL with named JPQL queries. JPQL doesn't support comments. If you want to control the query, you will have to use SQL, or use native EclipseLink functionality. See the 'SQL' function described here http://java-persistence-performance.blogspot.com/2012/05/jpql-vs-sql-have-both-with-eclipselink.html

Upvotes: 3

Related Questions