-->
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: Three table queries
PostPosted: Tue Nov 18, 2008 6:19 am 
Newbie

Joined: Tue Nov 18, 2008 6:08 am
Posts: 14
Hello everyone,

I am trying something like sql below for creating list from database,

Code:

select a.column1,c.column2 from A a, B b, C c where a.column1=b.column2 and b.column2=c.column2



now this query results a list successfully,
I tried to convert it to both models for table A and C but it was reporting exception as
Quote:
can't convert to model


all three has no relationship as far as concerned...

Please reply,
Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 18, 2008 7:03 am 
Regular
Regular

Joined: Wed Jun 20, 2007 1:53 am
Posts: 75
More than one objects/columns are projected in select clause and query return more results then HQL query will return List and that list inturns holds the object array (each index for a one column)

so we can iterate the list and get an object array


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 19, 2008 2:34 am 
Newbie

Joined: Tue Nov 18, 2008 6:08 am
Posts: 14
Thanks for your reply mate but below is my code with trace

Code:



public class Samplehiber {

    public static final Logger log = Logger.getLogger(Samplehiber.class);

    public List DeviceLastTrack(int mid) {
        List result;
        Session session = HibernetUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        result = session.createQuery("select l.deviceId,l.managerId,d.image from lastTrack l,device m,DeviceImage d where l.deviceId=m.deviceId and m.devicedescription = d.deviceType and m.managerId=1").list();
        t.commit();
        session.close();
        return result;
    }

    public static void main(String args[]) {
        Samplehiber sh = new Samplehiber();
        List result;
        result = sh.DeviceLastTrack(1);
        log.info(">>>>>>The Result is :" + result + " & size is " + result.size());
        for (int i = 0; i < result.size(); i++) {
            if (result.get(i) instanceof Model.lastTrack) {
                log.info(">>>>>>>Object " + i + " is instance of Model.lastTrack");
            } else if (result.get(i) instanceof Model.device) {
                log.info(">>>>>>>Object " + i + " is instance of Model.device");
            } else if (result.get(i) instanceof Model.DeviceImage) {
                log.info(">>>>>>>Object " + i + " is instance of Model.DeviceImage");
            } else {
                log.info(">>>>>>>Object " + i + " is indetermined");
            }
        }

    }
}


and the trace is

Quote:

Hibernate: select lasttrack0_.DeviceId as col_0_0_, lasttrack0_.ManagerId as col_1_0_, deviceimag2_.Image as col_2_0_ from lastTrack lasttrack0_, deviceMaster device1_, dimage deviceimag2_ where lasttrack0_.DeviceId=device1_.deviceid and device1_.devicedescription=deviceimag2_.DeviceType and device1_.managerId=1
11:48:46,093 INFO Samplehiber :>>>>>>The Result is :[[Ljava.lang.Object;@1fd6bea, [Ljava.lang.Object;@b7ec5d] & size is 2
11:48:46,093 INFO Samplehiber :>>>>>>>Object 0 is indetermined
11:48:46,093 INFO Samplehiber :>>>>>>>Object 1 is indetermined


I hope you got my problem,
It is unable to get conversion to any Model in iterator.
So, please Help, Thanks again


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 27, 2008 7:10 am 
Regular
Regular

Joined: Wed Jun 20, 2007 1:53 am
Posts: 75
you can write your for loop like below..note that this is sample only, dont try to execute the same..

for (int i = 0; i < result.size(); i++) {

Object []devices = result.get(i);
for (int j = 0; j < devices.length; j++) {
...deviceId = devices[j]
.....
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 29, 2008 2:24 am 
Newbie

Joined: Tue Nov 18, 2008 6:08 am
Posts: 14
Thanks For your Help Dude it really Helped....

so i am sending the precise soln to help that ones who need


Code:
for (int i = 0; i < result.size(); i++) {
            Object[] devices = (Object[]) result.get(i);
            for(int j=0;j<devices.length;j++){
            log.info(devices[j]);
               
        }
    }


eventually which prints my desired output


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.