-->
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: Getting 'phantom' results with many-to-many
PostPosted: Mon Jul 18, 2005 2:42 am 
Regular
Regular

Joined: Wed May 11, 2005 11:57 pm
Posts: 80
I have a many-to-many relationship between Users (called Level1Users) and the Clients (called Level2Clients) that they are assigned to. The mapping looks like this (I am only posting the parts that I think are relevant, if the complete mapping would be better then please let me know):

For Level1Users:
Code:
<list name="level2Clients" lazy="true" table="JoinLevel1UserLevel2Client">
   <key column="level1UsersId"/>
   <index column="sortOrderForlevel1Users"/>
   <many-to-many column="level2ClientsId" class="Level2Client"/>
</list>


For Level2Clients:
Code:
<list name="level1Users" lazy="true" table="JoinLevel1UserLevel2Client" inverse="true" >
   <key column="level2ClientsId"/>
   <index column="sortOrderForlevel2Clients"/>
   <many-to-many column="level1UsersId" class="Level1User"/>
</list>


I have a single row in each table for my test data (MySQL). The primary keys for both table are set to '1'. I have a single row join table, as shown here:

Code:
mysql> select * from Level1User;
+----+----------+------------+----------+--------------+----------------+--------------------------+-------------+
| id | userRole | savedState | personId | userObjectId | level1ClientId | sortOrderForlevel1Client | dateCreated |
+----+----------+------------+----------+--------------+----------------+--------------------------+-------------+
|  1 | NULL     | NULL       |        1 |            1 |              1 |                        0 | NULL        |
+----+----------+------------+----------+--------------+----------------+--------------------------+-------------+
1 row in set (0.00 sec)

mysql> select * from Level2Client;
+----+------+----------+-------------------+----------------+--------------------------+-----------------------------+
| id | name | personId | streamingServerId | level1ClientId | sortOrderForlevel1Client | sortOrderForstreamingServer |
+----+------+----------+-------------------+----------------+--------------------------+-----------------------------+
|  1 | foo  |        1 |                 1 |              1 |                        1 |                           1 |
+----+------+----------+-------------------+----------------+--------------------------+-----------------------------+
1 row in set (0.01 sec)

mysql> select * from JoinLevel1UserLevel2Client;
+---------------+-----------------+-------------------------+---------------------------+
| level1UsersId | level2ClientsId | sortOrderForlevel1Users | sortOrderForlevel2Clients |
+---------------+-----------------+-------------------------+---------------------------+
|             1 |               1 |                       1 |                         0 |
+---------------+-----------------+-------------------------+---------------------------+
1 row in set (0.00 sec)


When I execute the following code, I get two items in my list - the first one is null, and the second one is the actual client record.

Code:
session.clear(); //Make sure we're starting from scratch
List users = session.createCriteria( Level1User.class ).list();
Level1User firstUser = (Level1User)users.get(0);
List clients = firstUser.getLevel2Clients();
clients.size(); //This returns 2 instead of 1


Any ideas on why I'm getting this phantom null record? I'm expecting that I should only get the single Level2Client record that exists in the database.


Top
 Profile  
 
 Post subject: SOLVED
PostPosted: Mon Jul 18, 2005 2:39 pm 
Regular
Regular

Joined: Wed May 11, 2005 11:57 pm
Posts: 80
The problem was the sort order field in the join table. We had manually entered test data ourselves, and one of the sort fields was correctly set to 0, but the other was incorrectly set to 1. Apparently, Hibernate assumes that the sort order field is an accurate indicator of the number of records, and so it was creating the 'phantom record'. Setting the sort field to 0 fixed the problem.

Bottom line - if you're changing indexed collections from outside of Hibernate, be careful to maintain the correct sort orderings!


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.