How to disable the auto-generated comment for native query in JPA/Hibernate application with enabling hibernate.use_sql_comments on JBoss EAP 7

Solution Unverified - Updated -

Issue

When <property name="hibernate.use_sql_comments" value="true"/> is specified in persistence.xml for JPA/Hibernate application, the auto-generated comment /* dynamic native SQL query */ is automatically added for native query. For example:

@Path("/list")
public class ExampleResource {

    @PersistenceContext
    private EntityManager em;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<Person> getAll() {
        Query query = em.createNativeQuery("SELECT * FROM employee;");
        List<Person> result = query.getResultList();
        return result;
    }   
}

Then, the following query is executed with the auto-generated comment:

INFO  [stdout] (default task-1) Hibernate: /* dynamic native SQL query */ SELECT * FROM employee;

How do I disable the auto-generated comment for the native query? Because I would like to use the pg_hint_plan (= hinting phrases in a comment of special form given with the target SQL statement. The special form is beginning by the character sequence /*+ and ends with */. e.g. specify /*+ custom hint */ in front of SQL statement) with the native query, but the auto-generated comment prevents the pg_hint_plan works.

Of course, when hibernate.use_sql_comments is set to false (or just removing <property name="hibernate.use_sql_comments" value="true"/>), the auto-generated comment does not happen. However, I need to use the comment in other places. So, I do not want to disable the property.

Environment

  • Red Hat JBoss Enterprise Application Platform (EAP)
    • 7.x
  • JPA/Hibernate

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase of over 48,000 articles and solutions.

Current Customers and Partners

Log in for full access

Log In