Hello everybody,
I am struggling for a while now with a mapping problem: I want to realize a bidirectional many-to many relationship on the same entity.
I have a User that can have friends, which are of the type User as well. My mapping looks like that:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-cascade="all">
<class name="com.objecttrax.tracking.domain.User" table="user">
<id name="id" type="long">
<generator class="native" />
</id>
<property name="username" unique-key="true" />
<property name="password" />
<array name="friends" table="tracking_user_friends" inverse="true">
<key column="friend_id" />
<index column="id" />
<many-to-many column="user_id"
class="com.objecttrax.tracking.domain.User" />
</array>
<array name="friendOf" table="tracking_user_friends" cascade="none">
<key column="user_id" />
<index column="id" />
<many-to-many column="friend_id"
class="com.objecttrax.tracking.domain.User" />
</array>
</class>
</hibernate-mapping>
On the first look that works. I am testing the following scenario:
I have 3 users, which are persisted:
user1
user2
user3
I am adding the following on the friendOf site of the relationship of the users and persist it:
user1.friendOf => [user2,user3]
user2.friendOf => [user3]
After persisting I test the friends site of the relationship and I get this:
user1.friends => [] (OK)
user2.friends => [user1] (OK)
user3.friends => [user2] (
NOT OK) expected: [user1, user2]
It seems to me that index (<index column="id"/>) is causing the problem, but I don't know a better way for the mapping.
Does anybody has idea?
Thanks for your help.
Best regards,
Jens
---
Jens Laufer
http://www.objectworkz.org