Announcement Announcement Module
Collapse
No announcement yet.
why this.getHibernateTemplate().find show error result Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • why this.getHibernateTemplate().find show error result

    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!

  • #2
    Id has to be unique but in your table B it is not. You need to either make it unique or use <composite-id>.

    Also your mappings are not very (any) object oriented. You could for example create an instance of POJO A in your POJO Object B then map them with <one-one> or <many-one>.

    Comment

    Working...
    X