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.  [ 8 posts ] 
Author Message
 Post subject: escaping colons?
PostPosted: Wed Mar 30, 2005 5:42 pm 
Newbie

Joined: Wed Mar 30, 2005 5:31 pm
Posts: 5
Hibernate version:
2

I am trying to do a time zone converted query. In MySQL, I would write:
select time from auditevents where CONVERT_TZ(time, @@session.time_zone, '-08:00') >= '1970-01-02 00:00:00';

This query properly returns me one record.

I would like to do the same kind of query from hibernate. So, I try this:
results = session.createSQLQuery("select {event.*} from auditevents event where CONVERT_TZ(time, @@session.time_zone, '-08:00') >= '1970-01-02 00:00:00'", "event", AuditEvent.class).list();

Unfortunately, hibernate seems to be creating named parameters for me at the colons:
Hibernate: select event.id as id0_, event.type as type0_, event.time as time0_,
event.username as username0_, event.src_IP_high as src_IP_h5_0_, event.src_IP_low as src_IP_low0_ from auditevents event where CONVERT_TZ(event.time, @@session.time_zone, '-08?') >= '1970-1-2 00?'

Hibernate returns to me no results.

Is there any way to escape the colons, or some other solution to this problem?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 31, 2005 8:58 pm 
Newbie

Joined: Wed Mar 30, 2005 5:31 pm
Posts: 5
To clarify the version, I'm using Hibernate 2.1.7c to connect to MySQL 4.1.7 using Connector/J 3.1.7.

I've tried escaping as \\: and as ::.

I've also tried using different kinds of quotes around the times, double quotes, double single quotes.

None has worked.

I've also tried actually using a named parameter:
Code:
results = session.createSQLQuery("select {event.*} from auditevents event where :whereClause", "event", AuditEvent.class).setString("whereClause", "CONVERT_TZ(event.time, @@session.time_zone, '-08:00') >= '1970-1-2 00:00:00'").list();


Hibernate says it's running the following query:
Hibernate: select event.id as id0_, event.type as type0_, event.time as time0_,
event.username as username0_, event.src_IP_high as src_IP_h5_0_, event.src_IP_low as src_IP_low0_ from auditevents event where ?

But no results are returned.

Surely someone has tried doing queries with times in them before. Any help or suggestions would be greatly appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 01, 2005 3:00 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
does this fail in the last released h2 and h3 ?

i think we should be covered with respect to not picking up false : parameters.

otherwise pleae provide a simple failing testcase (hopefully something that can run without some specific function called ,)

/max

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 01, 2005 3:33 pm 
Newbie

Joined: Wed Mar 30, 2005 5:31 pm
Posts: 5
Still fails in Hibernate 2.1.8.

A simpler test case is:
Code:
results = runSQLQuery("select {event.*} from auditevents as event where event.time >= '1970-1-2 00:00:00'", "event", AuditEvent.class);


Which generates the following query:
Hibernate: select event.id as id0_, event.type as type0_, event.time as time0_,
event.username as username0_, event.src_IP_high as src_IP_h5_0_, event.src_IP_low as src_IP_low0_ from auditevents as event where event.time >= '1970-1-2 00?'

But due to some strangeness in MySQL, that query actually returns the proper number of results. I'm guessing that it can handle the slightly misshapen date (1970-1-2 00?) better than it can the misshapen timezone offset (-08?).

I will try Hibernate 3 next.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 01, 2005 5:29 pm 
Newbie

Joined: Wed Mar 30, 2005 5:31 pm
Posts: 5
Also fails in Hibernate 3.

Simple case:
Code:
Code:
results = runSQLQuery("select {event.*} from auditevents as event where event.time >= '1970-1-2 00:00:00'", "event", AuditEvent.class);


Output:
Hibernate: select event.id as id0_, event.type as type24_0_, event.time as time24_0_, event.username as username24_0_, event.src_IP_high as src5_24_0_, event.src_IP_low as src6_24_0_ from auditevents as event where event.time >= '1970-1-2 00?'

Complex case:
Code:
results = runSQLQuery("select {event.*} from auditevents event where CONVERT_TZ(event.time, @@session.time_zone, '-08:00') >= '1970-1-2 00:00:00'", "event", AuditEvent.class);


Output:
Hibernate: select event.id as id0_, event.type as type24_0_, event.time as time24_0_, event.username as username24_0_, event.src_IP_high as src5_24_0_, event.src_IP_low as src6_24_0_ from auditevents event where CONVERT_TZ(event.time, @@session.time_zone, '-08?') >= '1970-1-2 00?'


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 05, 2005 6:28 pm 
Newbie

Joined: Wed Mar 30, 2005 5:31 pm
Posts: 5
Well, I've discovered a workaround to my problem (send the timezone string, rather than the numeric offset). However, I strongly urge the Hibernate team to add some way to escape colons in SQL and HQL.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 05, 2005 7:01 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
any reason why you dont just put it in as a parameter ?

Much safer btw.

-max

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 06, 2005 6:07 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Have you tried to escape field names ?
results = runSQLQuery("select {event.*} from auditevents event where CONVERT_TZ( {event.time}, @@session.time_zone, '-08:00') >= '1970-1-2 00:00:00'", "event", AuditEvent.class);[/code]


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