views:

183

answers:

3

Hi,

can Hibernate generate uppercase SQL? I.e. some option which would make it send

SELECT table1_.prop1_ FROM my_table AS table1_;

instead of current

select table1_.prop1_ from my_table as table1_;

which I consider far less readable, esp. for long queries HBN tends to spit. See also https://forum.hibernate.org/viewtopic.php?f=1&t=932412

Thanks

+2  A: 

I don't know that this will enforce uppercase, but it does help with readability. In your hibernate.cfg.xml place the following in your <session-factory> element:

<!--hibernate.cfg.xml -->
<property name="format_sql">true</property>
Andy
No, this doesn't enforce uppercase...
Pascal Thivent
+2  A: 

I'm not aware of a way to upper case the keywords only out-of-the-box. But you could write your own interceptor and implement o.h.Interceptor#onPrepareStatement(String) to transform the sql string as you like.

Pascal Thivent
You'd need to do some serious parsing work though to avoid capitalizing stuff that should be left as is (for example the word "select" inside a like where clause).
cherouvim
@cherouvim: Indeed, it's not that trivial. But its clearly doable.
Pascal Thivent
Yes, doable. I've done it to add index hints. Probably not worth doing it though just for capitalizing. At least in production.
cherouvim
@cherouvim: I totally agree (using a "capitalizer interceptor" in production would generate useless overhead).
Pascal Thivent
+1  A: 

Found: The King of the Hibernate team is strictly against such option:

http://opensource.atlassian.com/projects/hibernate/browse/HB-1070

For anyone looking for this, please comment there (you can't vote on closed issues :( )

Update: Another solution is to replace the FreeMarker templates in hibernate's .jar's.

Ondra Žižka
I just love the comment :))
Pascal Thivent
Huh? FreeMarkers templates for SQL generation? That would be new to me (to my knowledge, Hibernate Tools uses templates, not Hibernate).
Pascal Thivent
Your're right, I confused it.
Ondra Žižka