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.