-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: createSQLQuery question about colons and times
PostPosted: Thu Apr 06, 2006 6:30 pm 
Newbie

Joined: Thu Apr 06, 2006 6:06 pm
Posts: 10
Hi I have a problem getting it to leave the SQL query alone. My best guess is that Hibernate wants to interpret the colon like it does with a Hibernate Query. My question is whether or not there is a way of getting it to treat the colon as a colon and not some special character?

Hibernate version: 3.0

Mapping documents: made from scratch like a geico quote?

Code between sessionFactory.openSession() and session.close(): yes

Name and version of the database you are using: Oracle 9i

Hibernate code:
Iterator i = session.createSQLQuery("SELECT " +
" TO_CHAR (rcpt_date, 'mm/dd/yyyy hh24:mi:ss') received," +
" TO_CHAR (rlsd_date, 'mm/dd/rrrr hh24:mi:ss') released," +
" TO_CHAR (shpd_date, 'mm/dd/rrrr hh24:mi:ss') shipped" +
" FROM aTable" +
" WHERE documentNumber LIKE " + 'SOMETHINGOROTHER%'")
.addScalar("RECEIVED", Hibernate.STRING)
.addScalar("RELEASED", Hibernate.STRING)
.addScalar("SHIPPED", Hibernate.STRING)
.list().iterator();

The generated SQL (show_sql=true):
SELECT
TO_CHAR (rcpt_date, 'mm/dd/yyyy hh24?') received,
TO_CHAR (rlsd_date, 'mm/dd/rrrr hh24?') released,
TO_CHAR (shpd_date, 'mm/dd/rrrr hh24?') shipped,
FROM aTable
WHERE documentNumber LIKE 'SOMETHINGOROTHER%'

The SQL I want:
SELECT
TO_CHAR (rcpt_date, 'mm/dd/yyyy hh24:mi:ss') received,
TO_CHAR (rlsd_date, 'mm/dd/rrrr hh24:mi:ss') released,
TO_CHAR (shpd_date, 'mm/dd/rrrr hh24:mi:ss') shipped,
FROM aTable
WHERE documentNumber LIKE 'SOMETHINGOROTHER%'


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 07, 2006 12:54 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I've gone through the parse code and I don't see support for escape characters. However you can get around it by making the pattern bit ('mm/dd/yyyy hh24:mi:ss') into a positional or named parameter, because there's no recursion in parsing:
Code:
Iterator i = session.createSQLQuery("SELECT " +
  " TO_CHAR (rcpt_date, :datetimePattern) received," +
  " TO_CHAR (rlsd_date, :datetimePattern) released," +
  " TO_CHAR (shpd_date, :datetimePattern) shipped" +
  " FROM aTable" +
  " WHERE documentNumber LIKE " + 'SOMETHINGOROTHER%'")
  .setString("datetimePattern", "mm/dd/yyyy hh24:mi:ss")
  .addScalar("RECEIVED", Hibernate.STRING)
  .addScalar("RELEASED", Hibernate.STRING)
  .addScalar("SHIPPED", Hibernate.STRING)
  .list().iterator();


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 07, 2006 2:40 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
use parameters as suggested - also generally more safe and efficient.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: Thanks, I'll go with that.
PostPosted: Fri Apr 07, 2006 10:30 am 
Newbie

Joined: Thu Apr 06, 2006 6:06 pm
Posts: 10
Tenwit,
Thanks for the swift reply. Thanks for checking to see if there was a valid escape sequence. I'll go with your suggestion.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.