aspose file tools*
The moose likes Object Relational Mapping and the fly likes Confusion when joining two tables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Confusion when joining two tables" Watch "Confusion when joining two tables" New topic
Author

Confusion when joining two tables

Venkat dasari
Ranch Hand

Joined: Nov 12, 2003
Posts: 67
Hi,
I am trying to write a query in .hbm.xml file where i am joining the tables emp and dept where deptno is the common column in both the tables .My query goes like this...

<![CDATA[
from Emp as e
join e.Deptno as dept
where e.Deptno = dept.Deptno
]]>

When i am trying to run my java program what kind of object will i get..
It returns java.lang.Object... what will i do with this?
I am li'll bit will someone please help me regarding this...

Thanks a lot,
venkat Dasari

SCJP1.4


Regards,<br />Venkat Dasari<br />SCJP 1.4, SCBCD.
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
Please post your mapping file (*.hbm.xml) and java source code.


SCJA,SCJP,SCWCD,SCBCD,SCEA I
Java Developer, Thailand
Wirianto Djunaidi
Ranch Hand

Joined: Mar 20, 2001
Posts: 210

It's because you are doing inner join so you end up with all the columns from both tables.
That's why hibernate return Object, because it doesn't know the what to return.

So what exactly do you expect? If you are expectign object Emp to be returned, try:


See if it works.
[ September 02, 2004: Message edited by: Wirianto Djunaidi ]
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312

It returns java.lang.Object... what will i do with this?



I think, you can cast type in your code.

you don't mind.
Venkat dasari
Ranch Hand

Joined: Nov 12, 2003
Posts: 67
my xml file:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping package="com.ats.axsis.service.workflow.hibpack2">
<class name="Emp" table="EMP">
<id
column="EMPNO"
name="Empno"
type="integer"
>
<generator class="vm" />
</id>
<property
column="SAL"
length="7"
name="Sal"
not-null="false"
type="integer"
/>
<property
column="HIREDATE"
length="7"
name="Hiredate"
not-null="false"
type="date"
/>
<property
column="COMM"
length="7"
name="Comm"
not-null="false"
type="integer"
/>
<property
column="JOB"
length="9"
name="Job"
not-null="false"
type="string"
/>
<property
column="MGR"
length="4"
name="Mgr"
not-null="false"
type="integer"
/>
<property
column="ENAME"
length="10"
name="Ename"
not-null="false"
type="string"
/>

<many-to-one
class="Dept"
name="Deptno"
not-null="true"
>
<column name="DEPTNO" />
</many-to-one>
</class>

<query name="com.ats.axsis.service.workflow.hibpack2.samplequery">
<![CDATA[
from Emp as e
where e.Empno = ?
and e.Deptno
=? and e.Deptno in (select d.Deptno from Dept as d where
d.Deptno = e.Deptno) ]]>

</query>

<query name="com.ats.axsis.service.workflow.hibpack2.Joinquery">
<![CDATA[
from Emp as e
join e.Deptno as d
where e.Deptno =d.Deptno
]]>

</query>


</hibernate-mapping>

my Java File:

public class EmpTest2 {

public static void main(String[] args) throws HibernateException{
_RootDAO.initialize();
Session session=_RootDAO.createSession();
try{
Query query=session.getNamedQuery(EmpDAO.QUERY_COM_ATS_AXSIS_SERVICE_WORKFLOW_HIBPACK2_JOINQUERY);

ListIterator iter = query.list().listIterator();
for(; iter.hasNext() {
Object o = iter.next();
System.out.println("class Name = " + o.getClass().getName());
try {
Emp e = (Emp) o;
Dept d = e.getDeptno();

System.out.println(d.getDeptno() + "::" + d.getDname());

} catch(Exception e) {
e.printStackTrace();
}
}
}

I know that this will throw ClassCastException....
but how can i get values from both the tables if i desire...

Thanks,
Venkat Dasari

Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
Please consider the answer of Wirianto Djunaidi. That is the answer :-).

./pope


blog - InfoQ.com
Venkat dasari
Ranch Hand

Joined: Nov 12, 2003
Posts: 67
hi,
i know that i can retrieve one table and i of course i successfully retrieved the values of one table.

But what if i want the values from two tables how do i retrieve ? it from columns which are in different tables and how can i cast it into their respective objects...that's the point of concentration...

thanks,
regards,
Venkat Dasari

SCJP 1.4
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
I guess what the list contains in fact is List<Object[]>. Try this. Every column object is contained in the object array.

./pope
Gavin King
author
Ranch Hand

Joined: Aug 31, 2004
Posts: 76
Yes, the query returns a List of ordered pairs of (Emp, Deptno) which Hibernate represents as Object[].


Co-Author of <a href="http://www.amazon.com/exec/obidos/ASIN/193239415X/ref=jranch-20" target="_blank" rel="nofollow">Hibernate in Action</a>
Venkat dasari
Ranch Hand

Joined: Nov 12, 2003
Posts: 67
hi,
Nice to see the co-author of Hibernate here...
I know that i will get an object array but what will i do with the object array which i get. I cant retrieve the colomn names etc...
This is in fact worrying me a lot I want to know how can this be resolved..
I never saw any examples in any sites showing how to insert values into the database. I want to know how this can be done through the mapping file.
Please, some one reply to this as soon as possible..
Thank you,

Regards,
venkatDasari
Venkat dasari
Ranch Hand

Joined: Nov 12, 2003
Posts: 67
hi everybody,
can anyone give me a solution for which i am in big doubt.
Now how t integrate weblogic and hibernate amd what are the steps to be followed?
thanks,
Regards,
Venkat Dasari
SCJP 1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confusion when joining two tables