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
|