-->
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: net.sf.hibernate.type.Type question
PostPosted: Fri May 07, 2004 7:08 am 
Newbie

Joined: Fri May 07, 2004 6:54 am
Posts: 1
hi,

by these select I have selected a list of net.sf.hibernate.type.Type object:

List typeList = session.find("SELECT B.id, B.name " +
"FROM Brand B, RelBrand RB WHERE RB.study = ?" +
"AND B.id = RB.brand.id", new Object[] { idStudy },
new Type[] { Hibernate.LONG, Hibernate.STRING });


How can I get java.lang.Long and java.lang.String from net.sf.hibernate.type.Type[] array, without mapping to object ??


Top
 Profile  
 
 Post subject: Re: net.sf.hibernate.type.Type question
PostPosted: Fri May 07, 2004 10:03 am 
Regular
Regular

Joined: Tue Jan 13, 2004 4:57 am
Posts: 83
Godsmack wrote:
hi,

by these select I have selected a list of net.sf.hibernate.type.Type object:

List typeList = session.find("SELECT B.id, B.name " +
"FROM Brand B, RelBrand RB WHERE RB.study = ?" +
"AND B.id = RB.brand.id", new Object[] { idStudy },
new Type[] { Hibernate.LONG, Hibernate.STRING });


How can I get java.lang.Long and java.lang.String from net.sf.hibernate.type.Type[] array, without mapping to object ??


Note that the role of the type array is not to specify the types of the params you receive in return, but the ones you are setting as parameters in the query!!
Quote:
/**
* Execute a query with bind parameters.
*
* Binding an array of values to "?" parameters in the query string.
*
* @param query the query string
* @param values an array of values to be bound to the "?" placeholders (JDBC IN parameters).
* @param types an array of Hibernate types of the values
* @see Hibernate for access to <tt>Type</tt> instances
* @return a distinct list of instances
* @throws HibernateException
*/


So if you are casting the result to Type[] objects it will probably give you a classcastexception. I don't remember what type you should be receiving as list elements, though, maybe Object[]?


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 07, 2004 11:26 am 
Beginner
Beginner

Joined: Tue Aug 26, 2003 9:50 am
Posts: 34
Location: Weiden Opf. / Germany
you will receive an Object[] containing java.lang.Longs and java.lang.Strings

besides that, i think your query won't run like this. you are missing a space at the end of the second line behind "study = ?" ;-)


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 07, 2004 11:37 am 
Beginner
Beginner

Joined: Tue Aug 26, 2003 9:50 am
Posts: 34
Location: Weiden Opf. / Germany
Actually while i'm looking at it, it won't run like this at all. Hibernate determines return types using the mapping files. you don't need to spacefiy them, in fact you can't.

And what about this RelBrand? Is it an attribute of Brand? Since i don't know for sure how they are related i just left this bit, but besides that your query should actually look like this:

List typeList = session.find(
"SELECT B.id, B.name FROM Brand B, RelBrand RB "
+ "WHERE RB.study = ? "
+ "AND B.id = RB.brand.id",
idStudy,
Hibernate.LONG);

(assuming that idStudy is a Long)

Only if you have several parameters you need the Object[] and Type[].

As the result of that query you will receive a List with Object[]s each containing an id and a name.

Maybe you might wanna do it like this instead:

List typeList = session.find(
"SELECT B FROM Brand B, RelBrand RB "
+ "WHERE RB.study = ? "
+ "AND B.id = RB.brand.id",
idStudy,
Hibernate.LONG);

which will return a List with the Brand objects you are looking for. This is more object oriented and how we usually do it.


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.