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.  [ 1 post ] 
Author Message
 Post subject: About using LEFT JOIN with native SQL
PostPosted: Tue May 20, 2008 6:45 am 
Newbie

Joined: Tue May 20, 2008 6:25 am
Posts: 1
Hi,

I have a native SQL like this:

select m.*, a.* from message left join attachment on m.id = a.id


when this SQL is executed in the DB, the following will be returned:

m.id | a.id
------------
1 1
2 2
3
4
5 3

I executed the SQL query as follow:

Session sess = sessionFactory.openSession();
SQLQuery query = sess.createSQLQuery(sql.toString()).addEntity("m", Message.class)
.addEntity("a", Attachment.class);
List parentList = query.list();



When I iterate through the list like this:

Iterator iterator = parentList.iterator();
while (iterator.hasNext()) {
Object[] object = (Object[]) iterator.next();
Message message = (Message) object[0];
Attachment attachment = (Attachment) object[1];
if(message!= null){
System.out.println("m.id: " + message.getId());
}else{
System.out.println("message is null");
}
if(attachment!= null){
System.out.println("a.id: " + attachment.getId());
}else{
System.out.println("attachment is null");
}
System.out.println("-----------");
}

the result is as follow:

1
1
---------
2
2
---------
message is null
attachment is null
---------
message is null
attachment is null
---------
5
3
---------


What I expected is that the message id for 3 and 4 should be shown instead of "message is null". Why message 3 and 4 is null? Anything I can do to solve this problem?

Thanks in advance

Queenie


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.