Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confusion when joining two tables

 
Venkat dasari
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post your mapping file (*.hbm.xml) and java source code.
 
Wirianto Djunaidi
Ranch Hand
Posts: 210
Ruby Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please consider the answer of Wirianto Djunaidi. That is the answer :-).

./pope
 
Venkat dasari
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the query returns a List of ordered pairs of (Emp, Deptno) which Hibernate represents as Object[].
 
Venkat dasari
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic