-->
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.  [ 3 posts ] 
Author Message
 Post subject: Escaping a SQL keyword in HQL - common solutions not working
PostPosted: Tue May 06, 2008 1:25 am 
Newbie

Joined: Thu Mar 09, 2006 12:31 am
Posts: 8
Hello-

I have a column named "End" in a table of mine that holds sessions. Since "End" is a reserved word in SQL Server 2005, I'm getting the following error when I use some HQL to query for a session where the "End" column is null:

Incorrect syntax near the keyword 'End'.

The bit of HQL that generates this is:

"from UserSession where UserId = :userId and End is null"

In the generated SQL, there is this snippet:

where (UserId=@p0 )and(End is null )

I've gotten this before on table names since I have a penchant for using the most natural name for something even if it clashes with a reserved word (User and Order typically). I've used the backtick notation to escape table names, but for some reason it isn't working in this case.

Here's what I've tried:

Changing the HQL to the following:

"from UserSession where UserId = :userId and `End` is null"

and

"from UserSession where UserId = :userId and [End] is null"

I've also tried changing my mapping file so it maps the property like this:

<property name="End" column="`End`" />

and

<property name="End" column="[End]" />

None of these work. They all throw the same error, except for when I use the square brackets in the HQL. In that case it throws an error saying there were unexpected brackets.

Does anyone know how to resolve this without renaming my column?

Regards-
Eric


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 06, 2008 1:38 am 
Newbie

Joined: Thu Mar 09, 2006 12:31 am
Posts: 8
I just tried something for kicks and it looks like it works. Here's what I did.

First, I change the mapping document to look like this:

<property name="End" column="`End`" />

Then, I changed my HQL to look like this:

"from UserSession us where us.UserId = :userId and us.End is null"

It took the combination of the backtick notation in my mapping document and using an alias for my class name to get it to work.

Does this feel a bit like a hack to anyone else?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 06, 2008 2:10 am 
Regular
Regular

Joined: Thu Mar 06, 2008 5:06 am
Posts: 68
Hi,
that's no hack, you're on the right way with your solution


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