-->
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: Native sql iBatis style
PostPosted: Fri Jul 09, 2004 2:10 pm 
Beginner
Beginner

Joined: Sun Sep 14, 2003 10:54 am
Posts: 36
Hi,

Sometimes it may be necessary to execute custom SQL that returns, e.g., one value or a List of values (e.g. a list of Strings or list of int values, etc). iBatis makes it very easy to execute this type of queries (see code below), without a need for any special mappings, etc.

What is the best way to do this in HB? Thanks

Code:
<select id="getInventoryQuantity" resultClass="int" parameterClass="string">

    select QTY as value from INVENTORY where ITEMID = #value#
 
</select>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 09, 2004 2:12 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Hibernate is a "full" ORM tool, iBatis is a "light" ORM tool. Both have their use and place. In Hibernate, you would do myInventory.getQuantity().

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 09, 2004 2:25 pm 
Beginner
Beginner

Joined: Sun Sep 14, 2003 10:54 am
Posts: 36
Yes, the example I used was not a very good one. This one is better:

Code:
<select id="getUniqueUsernameList" resultClass="string" parameterClass="account">
    select distinct USERNAME as value from SIGNON
</select>

The idea is that I do not want to drag back the whole SIGNON table (it could be fairly large and *wide*), I just want the list of unique user names that signed on.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 09, 2004 2:26 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
In Hibernate, use an HQL query.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 09, 2004 2:54 pm 
Beginner
Beginner

Joined: Sun Sep 14, 2003 10:54 am
Posts: 36
Quote:
In Hibernate, use an HQL query.

But then I would still be returning the list of Signon class instances, would I not?

If not, then it appears I'd have to make up a new class something like:
Code:
public class SignonUserName {
private String userName;
...
}


and then use it in the query:

Code:
<sql-query name="hbUniqueUserList">
    <return alias="sigonUserName" class="SignonUserName"/>
    select distinct {Signon}.USERNAME as value from SIGNON {Signon}
</sql-query>


Is that what you had in mind? Seems like a lot of work just to run a query.


Also, the SQL query must only use the classes that have been mapped to tables, correct?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 09, 2004 3:01 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
No, use projection. Please read the HQL documentation.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 09, 2004 8:13 pm 
Beginner
Beginner

Joined: Sun Sep 14, 2003 10:54 am
Posts: 36
christian wrote:
No, use projection. Please read the HQL documentation.

The point I'm trying to make is that the result of native SQL query can only be a class that has been mapped (or a list of the mapped class instances). It's impossible for the result of native SQL to return, say, an int/Integer or a list of Dates, etc.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 09, 2004 8:17 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
There is already a JIRA issue about "returning scalar types" from createSqlQuery(). Aside from the fact that "scalar type" is not really correct, this is where you have to put your vote.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


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.