-->
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: Join b/w two independent tables
PostPosted: Mon May 31, 2004 11:40 am 
Newbie

Joined: Mon May 31, 2004 4:11 am
Posts: 3
Location: Bangalore, India
Hi,

I am new to Hibernate, I tried to do a join b/w two tables(User & Role) I got the resultant list of objects.. but I am not able to retrieve the corresponding objects and fileds from the resultant list of objects. According to our db design specification there should not be foreign key relation b/w User and Role in db level.


The source code:

User.hbm.xml:
-------------------
<hibernate-mapping package="com.sample.hiber">
<class name="User" table="users">
<id name="logonId" column="logonId" type="string"
length="20" >
<generator class="assigned"/>
</id>
<property name="roleId" type="string" length="20"/>
<property name="name" type="string" length="40"/>
<property name="password" type="string" length="20"/>
<property name="emailAddress" type="string" length="40"/>
<property name="lastLogon" type="date"/>
</class>
</hibernate-mapping>

Role.hbm.xml:
-----------------
<hibernate-mapping package="com.sample.hiber">
<class name="Role" table="roles">
<id name="roleId" column="roleId" type="string" length="20">
<generator class="assigned"/>
</id>
<property name="description" type="string" length="40"/>
</class>
</hibernate-mapping>

The above two are mappings for User and Role table.

How to achive the below specified sql query using HQL...?
"select usr.name, usr.emailAddress, rol.description from users as usr, roles as rol where usr.roleId=rol.roleId" (This is the normal SQL).


I tried as below:
------------------
Query usrQuery = session.createQuery("select usr, rol from User as usr, Role as rol where usr.roleId=rol.roleId");
List lst_users = usrQuery.list();

for (Iterator it = lst_users.iterator(); it.hasNext();)
System.out.println("Test:" + it.next().toString() );

Output:
---------
Test:[Ljava.lang.Object;@6cc26cc2
Test:[Ljava.lang.Object;@6ebb6ebb

I am unable to find what class type is this and how to retieve the fields of User and Role from this resultant list ?

Thanku for your time..

Regards,
-Jags

_________________
In a world without fences, who needs GATES??


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 31, 2004 11:51 am 
Senior
Senior

Joined: Sun Mar 14, 2004 10:16 am
Posts: 129
Location: Ankara
to get instances of the ur classes from that query result, you need to apply issues of mapping objects into relational databases.
Mapping relationships;
One-to-one
One-to-many
Many-to-one
Association vs agregation

If u use one of them according to ur objects, relational type of object mapping handles your problem.

_________________
-developer


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 31, 2004 4:16 pm 
Beginner
Beginner

Joined: Mon Sep 29, 2003 10:32 pm
Posts: 35
Location: Toronto, Ontario
The list contains an array of type Object[] with size 2.
Object[] a = (Object[]) it.next();

a[0] is a User, a[1] is a Role


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 01, 2004 2:47 am 
Newbie

Joined: Mon May 31, 2004 4:11 am
Posts: 3
Location: Bangalore, India
Thanku very much..
It worked.

-Jags


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.