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.  [ 7 posts ] 
Author Message
 Post subject: createSQLQuery returns invalid column name
PostPosted: Mon Jun 28, 2004 6:25 pm 
Newbie

Joined: Mon Jun 28, 2004 5:09 pm
Posts: 2
I have the following simple code:
String sql = "SELECT {unit}.PICK_CODE AS {unit.pickCode} FROM UNIT {unit}";
List resorts = session.createSQLQuery(sql, "unit", Unit.class).list();

My UNIT table has 3 columns:
ID
PICK_CODE
FLOOR_NAME

My Unit.hbm.xml is correct as well as the Unit.class, because when I run the above SQL with {unit.*}, it works just fine.

But I only want to return the one column. If I run the above code, I get an "Invalid column name" error returned. When I turned on the JDBC trace, I noticed that Hibernate was trying to perform findColumn(ID0) to find the ID column of table UNIT.

After looking through the docs and forums, I discovered that "...if you list each property explicitly, you must include all properties of the class and its subclasses!". This didn't make much sense because HQL's find() will return an Object[] in this situation, so why can't createSQLQuery()? Also, why can't the method just catch the JDBC's findColumn() exception and stick in a NULL into Unit.class remainder fields.

Now you may ask, why not just use HQL to do this? Well, because what I'm really trying to do is this SQL statement: SELECT DISTINCT SUBSTR(U.PICK_CODE,1,2) FROM UNIT U. Please the Hibernate team can explain this, I'd appreciate it!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 29, 2004 4:39 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
createSQLQuery is here to let you do SQL but fill in an object and attach it to the sesion.
If you want proejction and SQL, then une the underlying connection.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 30, 2004 11:07 am 
Newbie

Joined: Mon Jun 28, 2004 5:09 pm
Posts: 2
yes i know that createSQLQuery() is supposed to fill in an object, but what object?. From my experiment, it seems to be filling the Unit.class object, rather than just return a String object (or Object class casted from a String), just as HQL's find() method does.

Also, I didn't understand your second sentence. Please elaborate.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 06, 2004 11:17 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Should say, fill a persistent object (mapped one)
projection means select a.id, a.name instead of select a.
If you absolutly need to do projection using a native SQL query you can use session.connection()

_________________
Emmanuel


Top
 Profile  
 
 Post subject: what about..
PostPosted: Thu Dec 23, 2004 7:14 am 
Newbie

Joined: Thu Dec 23, 2004 7:03 am
Posts: 5
Location: Brasov, Romania
I understand your explanation, but in the book "Hibernate in action" on page 284 there is a code-example
Code:
String sql = "select u.USER_ID as {uzer.id},"
+ " u.FIRSTNAME as {uzer.firstname},"
+ " u.LASTNAME as {uzer.lastname} from USERS u";
List results =
session.createSQLQuery(sql, "uzer", User.class).list();
which is a projection. The table USER in this example is part of the Caveat Emptor application, and this table contains many other columns. Doesn't that means that it should work? Can you explain the meaning of this code, maybe I misunderstood it.
Thank you![/code]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 23, 2004 9:24 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
This is not a projection, it is a query for a full User object.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 23, 2004 1:46 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
all user proeprties are mandatory

_________________
Emmanuel


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