-->
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.  [ 5 posts ] 
Author Message
 Post subject: Many-to-many with one list in each class
PostPosted: Thu Apr 08, 2004 12:49 pm 
Regular
Regular

Joined: Thu Feb 05, 2004 6:51 am
Posts: 50
Hi all,

Hibernate 2.1.1 on servlet on JonAS 3.3.5

I have already tried to look for answers in this forum and the FAQ, but I can't figure out how I have to proceed.

I have two classes Group and User. In Group I have a list of users and in User I have a list of the groups where the user is registered.

Here is the mappings:
Code:
   <class name="HibernateUser" table="usertable">
        <list name="groups" table="usergrouptable" lazy="false">
            <key>
                <column name="uid"/>
            </key>
            <index column="j" type="integer"/>
            <many-to-many class="org.enhydra.shark.usergroup.HibernateGroup">
                <column name="gid"/>
            </many-to-many>
        </list>
    </class>


Code:
    <class name="HibernateGroup" table="grouptable">
       <list name="users" table="usergrouptable" lazy="false">
            <key>
                <column name="gid"/>
            </key>
            <index column="i" type="integer"/>
            <many-to-many class="org.enhydra.shark.usergroup.HibernateUser">
                <column name="uid"/>
            </many-to-many>
        </list>
    </class>


And here are the tables schema:
Code:
--for the association tables
CREATE TABLE "usergrouptable" (
    "uid" character varying(254) NOT NULL,
    "gid" character varying(254) NOT NULL,
    "i" integer,
    "j" integer,
    CONSTRAINT "FK_UserGroupTable_gid_GroupTable" FOREIGN KEY (gid) REFERENCES grouptable(gid) ON UPDATE NO ACTION ON DELETE NO ACTION,
    CONSTRAINT "FK_UserGroupTable_uid_UserTable" FOREIGN KEY (uid) REFERENCES usertable(uid) ON UPDATE NO ACTION ON DELETE NO ACTION,
    CONSTRAINT "PK_UserGroupTable" PRIMARY KEY ("uid","gid")
);


Code:
--for the User
CREATE TABLE "usertable" (
    "uid" character varying(254) NOT NULL,
    "name" character varying(254) NOT NULL,
    "passwd" character varying(254) NOT NULL,
    "email" character varying(254) NOT NULL,
    CONSTRAINT "PK_UserTable" PRIMARY KEY ("uid")
) ;


Code:
--for the User
CREATE TABLE "grouptable" (
    "gid" character varying(254) NOT NULL,
    "description" character varying(254) NOT NULL,
    CONSTRAINT "PK_GroupTable" PRIMARY KEY ("gid")
);


I have a in Group method to addUserToGroup (and this was to try a method in User to addGroupToUser). But when I addUserToGroup, it does not update properly the index column in the table and so the list of groups couldn't retrieve properly too.

What I do for the business code side is:
Code:
createUser("groupessai","useressai1","pass","rerfqze","dfsdfd");  //this include a call to groupessai.addUser(useressai1)
addUserToGroup("group1","useressai1");
addUserToGroup("group2","useressai1");
createUser("groupessai","useressai2","pass","rerfqze","dfsdfd");  //this include a call to groupessai.addUser(useressai2)
createUser("groupessai","useressai3","pass","rerfqze","dfsdfd");  //this include a call to groupessai.addUser(useressai3)



Here is the result in the association table :
uid gid i j
useressai1 groupessai NULL 0
useressai1 group1 0 NULL
useressai1 group2 0 NULL
useressai2 groupessai NULL 0
useressai3 groupessai NULL 0


So the question is does hibernate allow to have such a configuration and if yes how have I to do in order to make it work.

Thanks in advance for possible response.

Vlad


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 08, 2004 4:44 pm 
Regular
Regular

Joined: Thu Feb 05, 2004 6:51 am
Posts: 50
Thing is that when the update are performed by hibernate on the association table, there are done only on one of the index because hibernate has no idea of the other (I mean the one related to the other class, Group for example if we were updating a User).

So is there a way to work arount this?

Vlad


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 09, 2004 3:44 am 
Regular
Regular

Joined: Thu Feb 05, 2004 6:51 am
Posts: 50
Ok, I have just found in this forum the exact same case than me:
http://forum.hibernate.org/viewtopic.ph ... manytomany

But I have the same since I put inverse="true" for the list groups in the HibernateUser class.

So if the guj of the topic still read this forum, how did you make it?

Vlad


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 09, 2004 4:51 am 
Regular
Regular

Joined: Thu Feb 05, 2004 6:51 am
Posts: 50
Ok like I said, I had the inverse="true" for the groups attributes in User class and when I add a user to a group, I also add the group to the user and update manually both.

When I retrieve the users just after having created them, now it finds the groups where the user are registered, but if I try to get the same user later in another session, I get a "null index column for collection exception" which is normal because the index in the table hasn't been updated and is still set to null.

Table content now:
uid gid i j
useressai1 groupessai 0 NULL
useressai1 group1 0 NULL
useressai1 group2 0 NULL
useressai2 groupessai 1 NULL
useressai3 groupessai 2 NULL


Any idea?

Vlad


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 09, 2004 5:24 am 
Regular
Regular

Joined: Thu Feb 05, 2004 6:51 am
Posts: 50
I made some more testing and the index which is not updated even if for every operation on one of the lists I do the reverse operation (like user.add(group) and group.add(user)) is the one whose mapping is set to inverse="false".

So is this a bug, I don't think, so is this the expected behavior?

Vlad


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