-->
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.  [ 2 posts ] 
Author Message
 Post subject: why this.getHibernateTemplate().find show error result
PostPosted: Sun Nov 25, 2007 10:57 am 
Newbie

Joined: Fri Jun 08, 2007 3:16 am
Posts: 11
I have two table,table A and table B,
table A
Code:
ID     NAME
---------------
test   John
tiger  Kate


table B
Code:
ID  USERID  DESCRIPTION
-----------------------
b1  test     this is a test
b1  tiger    this is a tiger
b2  abc      this is a abc


A.hbm.xml
Code:
<class name="AModel" table="A">
  <id name="id" column="id" type="java.lang.String">   
    <generator class="assigned"/>
  </id>
  <property name="uname" type="java.lang.String">
    <column name="name" length="18" not-null="true" />
  </property>
</class>


B.hbm.xml
Code:
<class name="BModel" table="B">
  <id name="id" column="id" type="java.lang.String">   
    <generator class="assigned"/>
  </id>
  <property name="uid" type="java.lang.String">
    <column name="userid" length="100" not-null="false" />
  </property>
  <property name="g" type="java.lang.String">
    <column name="description" length="100" not-null="false" />
  </property>
</class>


AModel.java
Code:
public class AModel{
private String id;
private String uname;
....
}


BModel.java
Code:
public class BModel{
private String id;
private String uid;
private String g;
....
}


Then I execute following statement:
Code:
List info=this.getHibernateTemplate().find("from AModel a,BModel b where b.uid=a.id and b.id=?",'b1');
AModel a;
BModel b;
if(info.size()>0){
for(int i=0;i<info.size();i++){
Object[] obj=(Object[])info.get(i);
for(int j=0;j<obj.length;j++){
(obj[j] instanceof BModel){
  b=(BModel)obj[j];
  System.out.print(b.getUid());
  System.out.print(b.getG());
}
if(obj[j] instanceof AModel){
  a=(AModel)obj[j];
  System.out.println(a.getUname());
}
System.out.println("");
}


It should print following result:
Code:
test   John   this is a test
tiger  Kate   this is a tiger


above result is the result which I wish to get! But it prints following error result:
Code:
test   Kate   this is a tiger
tiger  Kate   this is a tiger


I don't know why it show correct result? I am puzzled it for several days! Anyone could point out where wrong with me?
How to get correct my code to get correct result:
Code:
test   John   this is a test
tiger  Kate   this is a tiger


Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 25, 2007 12:27 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Your database/data doesn't match your mapping files. You define table B as having a primary key column "id" but in your test data there are 2 rows with the id=b1 i.e. not a primary key. This discrepency will cause unusual behaviour.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.