-->
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: Assistance with converting many-to-many to one-to-many
PostPosted: Thu May 25, 2006 8:33 pm 
Beginner
Beginner

Joined: Mon Aug 15, 2005 11:50 pm
Posts: 22
I am wanting to convert a many-to-many mapping to a one-to-many link table, one-to-many style connection

I am attempting to convert a Project-ProjectUsers-User style connection where ProjectUsers is a link table but I am suffering from stupid user syndrome.

So if I have the following mapping (again unsure if this is correct).
How do I simply add users to a Project.




Code:

<id name="Project" column="ProjectId" type="Int32" unsaved-value="0">
      <generator class="native"/>
    </id>

<bag name="ProjectUsers" cascade="all-delete-orphan" lazy="true" inverse="true">
      <key column="ProjectId" />
      <one-to-many class="ProjectUser, ProjectUser" />
    </bag>

-------------------------------------------------------------------------------------------

<id name="ProjectUser" column="ProjectUserId type="Int32" unsaved-value="0"
   <generator class="native" />
</id>
<many-to-one name="Project" column="ProjectId" class="Project,Project" />
<many-to-one name="User" column="UserId" class="User, User" />


-------------------------------------------------------------------------------------------


<id Usercolumn="UserId" type="Int32" unsaved-value="0">
      <generator class="native"/>
    </id>

<bag name="UserProjects" cascade="all-delete-orphan" lazy="true" inverse="true">
      <key column="ProjectUserId" />
      <one-to-many class="ProjectUser, ProjectUser" />
    </bag>

-------------------------------------------------------------------------------------------



If anyone could point me to some sample code that is doing many-to-many via two one-to-many's that would be great.

Thanks,
Jason




Top
 Profile  
 
 Post subject:
PostPosted: Thu May 25, 2006 10:46 pm 
Beginner
Beginner

Joined: Mon Aug 15, 2005 11:50 pm
Posts: 22
//Background.
Checkbox list on form that person checks which users are part of project.
The list is the interrogated to determine which users to add to the project.

Code:
  IList projectUsers = GetAllprojectUsersForProjectId(ActiveProject.ProjectId);

            foreach (ListItem li in CheckBoxList.Items)
            {
                bool inDbSet = false;
                int projectUserId = -1;
                int userId = Convert.ToInt16(li.Value);

                User user = GetuserById(userId);

                foreach (ProjectUser  projectUser in projectUsers)
                {
                    if (projectuser.User.UserId == userId)
                    {
                        inDbSet = true;
                        projectUserId = projectuser.ProjectuserId;
                    }
                }

                if (li.Selected)
                {
                    //If Item selected and not already in Collection add new item and save.
                    if (!inDbSet)
                    {
                        Projectuser projectuser = new Projectuser();
                        projectuser.Project = ActiveProject;
                        projectuser.user = user;
                        Db.Save(projectuser);
                    }
                }
                else
                {
                    //Not selected but in set, delete item from set.
                    if (inDbSet)
                    {
                        if (projectUserId != -1)
                        {
                            ProjectUser projectuser =
                                (ProjectUser)Db.GetObjectById(typeof(ProjectUser), projectUserId);
                            Db.Delete(projectuser);
                        }
                    }
                }
            }


This is the cooky sort of code I've cooked up representing the presense of a confused soul. It works it just feels messy and excessive.

What I am having diffuclty with is writing code like the simple project.Users.Add(User) method that was achievable with the <many-to-many> configuration had previously.


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.