-->
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.  [ 5 posts ] 
Author Message
 Post subject: Retrieve the data from two related tables
PostPosted: Thu Feb 19, 2004 4:09 pm 
Newbie

Joined: Wed Jan 28, 2004 5:21 pm
Posts: 18
Location: Maracaibo, VZLA
Hi, I'm new using Hibernate, and I have a problem, I have two tables, Country and State, of course they have a relationship, I've already created it mapping file and java class, I used middlegen and hbm2java to do that. Well, I know how can I retrieve all Countries from my data base

List res = session.find("from Country")
for (Iterator i = res.iterator(); i.hasNext(); )
{
Country listCountries = (Country) i.next();
System.out.print("Id:"+listCountries.getIdCountry()+" Descripcion:"+listCountries.getDeCountry());
}


This code print all the countries that I have in the data base without any problem.... But I want to retrieve all countries with its states and I don't know how to do it !!!

I tried to do it, with this source code

List res = session.find("select country.idCountry, country.deCountry, state.idState, state.deState from Country as country inner join country.states as state")

And I created a class (value object) that has this four attribute
private Integer idCountry;
private String deCountry;
private Integer idState;
private String deState;
and its getter and setter of course....

and I cast the result to the new class
for (Iterator i = res.iterator(); i.hasNext(); )
{
CountryState listCountrieState = (CountryState) i.next();
System.out.print("Id:"+listCountries.getIdCountry()+" Descripcion:"+listCountries.getDeCountry());
}


but when I run it, it throw me the following error: java.lang.ClassCastException

Does anybody know how can I do what I want (I want to retrieve all countries with its states) ?? any help please !!!

Thanks in advance
Alejandro.-


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 19, 2004 4:31 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You should just use an association, Hibernate will automatically populate it. Take a look at some tutorials for example here: http://www.gloegl.de/15.html


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 19, 2004 7:20 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
if you mean having al the countris with their states in 1 request, then it's
Code:
from Country country inner join fetch country.states


but as Michael said, read the reference docs and tutorials : you have to make your ideas more clear on how hibernate work.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2004 9:52 am 
Newbie

Joined: Wed Jan 28, 2004 5:21 pm
Posts: 18
Location: Maracaibo, VZLA
Hi Enmanuel, I'm trying to do what you said me
from Country country inner join fetch country.states
and hibern8ide throw me the following message
(net.sf.hibernate.QueryException: fetch may not be used with scroll() or iterate())

I'm using SQLServer 2000, Do you know what could happening ? What am I doing wrong ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2004 10:50 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
If you want to retrieve all objets in 1 request, then use list() or find() anyway

_________________
Emmanuel


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