Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Add SQL query comments with JPA and Hibernate
PostPosted: Tue Apr 18, 2017 3:29 pm 
Newbie

Joined: Tue Apr 18, 2017 3:23 pm
Posts: 2
Hi,

is there a way to add a custom SQL comment to the generated SQL from Hibernate? For example, I would
like to add a SQL comment to SELECT statements that would eventually get executed by a Postgres
database.

Example:

/* t95=firstdraft */select foo, bar.....from ....

regards


Top
 Profile  
 
 Post subject: Re: add SQL comment to generated SQL
PostPosted: Tue Apr 18, 2017 11:56 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1172
If you are using the Hibernate native API, then you need to use the Query#setComment method like this:

Code:
List<Person> persons = (List<Person>) session.createQuery(
    "select p " +
    "from Person p ")
.setComment( "t95=firstdraft" )
.list();


If you are using JPA, then you can use a QueryHint:

Code:
List<Person> persons = entityManager.createQuery(
        "select p " +
        "from Person p ", Person.class)
.setHint( "org.hibernate.comment", "t95=firstdraft" )
.getResultList();

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
 Post subject: Re: Add SQL query comments with JPA and Hibernate
PostPosted: Mon Apr 24, 2017 12:53 pm 
Newbie

Joined: Tue Apr 18, 2017 3:23 pm
Posts: 2
many thanks!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.