-->
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.  [ 4 posts ] 
Author Message
 Post subject: Inconsistent results with many-to-one.
PostPosted: Sun Jan 04, 2004 12:54 pm 
Newbie

Joined: Sun Jan 04, 2004 12:26 pm
Posts: 3
Location: Kuala Lumpur, Malaysia
Hi,

I am using Hibernate 2.0.3 with DB2 8.1.3.

I came across a very strange behaviour with the many-to-one tag.

I used the following stand-alon example and it works fine:

Parent.hbm.xml:
--------------------
<hibernate-mapping>
<class name="hibtest.Parent" table="parent">
<id name="parentId" column="parentid" >
<generator class="assigned" />
</id>
<property name="parentName" type="string" />
<bag name="children"
inverse="true"
lazy="false"
order-by="childId"
cascade="all">
<key column="parentId"/>
<one-to-many class="hibtest.Child"/>
</bag>
</class>
</hibernate-mapping>

Child.hbm.xml:
------------------
<hibernate-mapping>
<class name="hibtest.Child" table="child">
<id name="childId" column="childid" >
<generator class="assigned" />
</id>
<property name="childName" type="string" />
<many-to-one name="Parent" column="parentId" not-null="true" />
</class>
</hibernate-mapping>

Database query:
---------------------
List list = session.find("from Child where childName = 'Boneeto'");

The above gets me the result I expected. i.e. if:
Table Parent
----------------
parentId: AAA, parentName: aaa
parentId: BBB, parentName: bbb
parentId: CCC, parentName: ccc

Table Child
--------------
parentId: AAA, childId: zzz, childName: Boneeto
parentId: BBB, childId: yyy, childName: Boneeto

then I get the Parents: AAA and BBB.


However, when I adapted to it to my own requirements:

<!-- this is my version of the Child. (only has 2 fields) -->
<hibernate-mapping>
<class name="UserRoleRelation" table="userrolerelation">
<id name="labsRoleId" column="labsRoleId" >
<generator class="assigned" />
</id>
<many-to-one name="LabsUser" column="labsUserId" not-null="true" /> <!-- this is equivalent to the Parent -->
</class>
</hibernate-mapping>

I only get the first set of LabsUser (Parent) no matter how many matching records in LabsUser and UserRoleRelation.

Any ideas what could be wrong?

Regards,
Vincent.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 04, 2004 2:26 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Full mapping and data in your DB ?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 04, 2004 10:56 pm 
Newbie

Joined: Sun Jan 04, 2004 12:26 pm
Posts: 3
Location: Kuala Lumpur, Malaysia
The data in table LabsUser (the Parent) contains:

Code:
LABSUSERID  FIRSTNAME             LASTNAME
----------  --------------------  --------------------
robi        Robert                HAUF
schur       Frank                 SCHUR
razali      Razali                AHMAD
loh         Vincent               LOH
duncan      Tim                   DUNCAN
garnett     Kevin                 GARNETT
robinson    David                 ROBINSON
allen       Ray                   ALLEN
stockton    Joh                   STOCKTON

The full mapping for LabsUser.hbm.xml is:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
   <class name="hibtest.LabsUser" table="labsuser">
      <id name="labsUserId" column="labsuserid">
          <generator class="assigned"/>
      </id>
      <property name="firstName" type="string" />
      <property name="lastName" type="string" />
      <bag name="userRolesList" inverse="true" lazy="false" cascade="all" order-by="labsRoleId">
         <key column="labsUserId" />
         <one-to-many class="hibtest.UserRoleRelation" />
      </bag>
   </class>
</hibernate-mapping>


The data in table UserRoleRelation (the Child) contains:

Code:
LABSROLEID  LABSUSERID
----------  ----------
CR          loh
AD          loh
AP          loh
PC          loh
AD          robi
AP          robinson
PC          duncan
AP          stockton
PC          garnett
PC          allen

The full mapping for UserRoleRelation.hbm.xml is:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
   <class name="hibtest.UserRoleRelation" table="userrolerelation">
      <id name="labsRoleId" column="labsRoleId" >
         <generator class="assigned" />
      </id>
      <many-to-one name="LabsUser" column="labsUserId" not-null="true" />
   </class>
</hibernate-mapping>


When I execute the following:

Code:
Session session = null;

try {
   session = sf.openSession();

   List lp = session.find("from UserRoleRelation where labsRoleId like 'PC'");

   for (int i = 0; i < lp.size(); i++) {
      System.out.println(((UserRoleRelation) lp.get(i)).getLabsUser().getLabsUserId());
   }
} catch (Exception e) {
   e.printStackTrace();
} finally {
   if (session != null)
      session.close();
}


It retrieves only the LabUserId = 'loh' even though there are 4 items in the list.

_________________
Rgds,
Vincent


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 05, 2004 12:27 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Hum, as you said to Hibernate, LABSROLEID is a primary key, and thus is unique in the table... So it will read the first result and stop. You misdeclare your primary key

_________________
Emmanuel


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