-->
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.  [ 2 posts ] 
Author Message
 Post subject: how to get data in Java when using a join of 2 tables
PostPosted: Wed May 12, 2010 7:51 pm 
Newbie

Joined: Tue May 11, 2010 6:38 pm
Posts: 5
Hi,
i have been learning Hibernate for the couple of days and trying to create a sample application
but have stumbled onto an issue for which i couldn't find any answers( even after searching the forums).

Problem:

the following query joins two tables and return the results.
Code:
List  l3 = s.createQuery("from Student as stud inner join stud.standard as std").list();



Hibernate Query executed at the backend
Code:
select student0_.id as id0_0_, standard1_.id as id3_1_, student0_.studentId as studentId0_0_, student0_.name as name0_0_, student0_.age as age0_0_, student0_.gender as gender0_0_, student0_.standard as standard0_0_, standard1_.classId as classId3_1_, standard1_.standard as standard3_1_, standard1_.section as section3_1_ from Students student0_ inner join standards standard1_ on student0_.standard=standard1_.id


Now my issue is how to iterate though the result obtained ?
i cannot cast the list to particular domain object as it returns data from both the entities.
please help!


Top
 Profile  
 
 Post subject: Re: how to get data in Java when using a join of 2 tables
PostPosted: Thu May 13, 2010 6:55 am 
Beginner
Beginner

Joined: Fri Nov 24, 2006 10:33 am
Posts: 42
Your query should return a List of Student objects. In your Student object you would have a variable of type Standard, and you could define the relationship like this

Account {
...
@OneToOne(fetch=FetchType....)
@JoinColumn(name="id", nullable=true)
private Standard standard;
...
}

(Your Standard class would also need to be an entity)

As you iterate through the returned student objects, you can access student.getStandard() and it will have been populated (assuming your FetchType is EAGER or you are still within the current transaction.


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