-->
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.  [ 3 posts ] 
Author Message
 Post subject: help with mapping
PostPosted: Wed May 18, 2005 9:05 am 
Newbie

Joined: Wed May 18, 2005 8:56 am
Posts: 5
Location: tibro, sweden
I have 3 tables in my database
tik2005_users
--------------
UserID int PK
Email nvarchar
name nvarchar
password nvarchar

tik2005_roles
---------------
roleID int PK
RoleName nvarchar

tik2005_userRoles
--------------------
UserID
RoleID

I have two classes
user.vb
Code:
Dim _userid As Integer
Dim _Name As String
Dim _password As String
Dim _email As String
Dim _roles As IList


roles.vb
Code:
Dim _roleID As Integer
Dim _roleName As String
Dim _userID As Integer


mapping for user
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
  <class name="tik2005.tik2005.user, tik2005" table="tik2005_users">
    <id name="userId" type="integer" unsaved-value="0">
      <generator class="identity" />
    </id>
    <property name="Email" type="String" length="40" />
    <property name="Password" type="String" length="20" />
    <property name="Name" type="String" length="40" />
    <bag name="Roles" table="tik2005_userroles">           
      <key column="userID" />           
      <many-to-many column="roleId" class="tik2005.tik2005.Roles, tik2005" />       
    </bag>
   </class>
</hibernate-mapping>


mapping for roles
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
    <class name="tik2005.tik2005.Roles, tik2005" table="tik2005_roles">
        <id name="roleId" type="integer" unsaved-value="0">
            <generator class="identity" />
        </id>
        <property name="roleName" type="String" length="50"/> 
    </class>
</hibernate-mapping>


When i get all the users from the database i also get all the roles that a user is in.
Now i tried with cascade="delete" but when i delete the user all the roles that the user is in get deleted to. Both from the roles table and the userroles table.

How should i map the tbales so when i delete a user or role all the posts in the userroles table withe the same user or roleID gets deleted?


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 19, 2005 12:35 am 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
You are very close to your solution. You need to turn on the cascade feature. By default, the cascade is "none" so in effect, none of the changes to your collection will be persisted. However, keep in mind that cascading "deletions" in many-to-many relationships will probably produce unexpected results. If you delete a User, do you really want the Role object to be deleted? Probably not, you probably just want the association.

Change your mapping file for user like so:

Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
  <class name="tik2005.tik2005.user, tik2005" table="tik2005_users">
    <id name="userId" type="integer" unsaved-value="0">
      <generator class="identity" />
    </id>
    <property name="Email" type="String" length="40" />
    <property name="Password" type="String" length="20" />
    <property name="Name" type="String" length="40" />

    <bag  name="Roles"
              cascade="save-update"
              table="tik2005_userroles">           
      <key column="userId" />           
      <many-to-many column="roleId" class="tik2005.tik2005.Roles, tik2005" />       
    </bag>
   </class>
</hibernate-mapping>


the addition of the cascade attrubute tells NHibernate to persist additions, and updates to the "Roles" collection. Additionally, deleting a User will automatically delete all "UserRoles" from the association table.

In order to have NHibernate delete all instances of the user assocations from the Role side of things, you need to add a collection mapping to the role object like so:

Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
    <class name="tik2005.tik2005.Roles, tik2005" table="tik2005_roles">
        <id name="roleId" type="integer" unsaved-value="0">
            <generator class="identity" />
        </id>
        <property name="roleName" type="String" length="50"/> 
    </class>

    <bag  name="Users"
              inverse="true"
              cascade="save-update"
              table="tik2005_userroles">           
      <key column="roleID" />           
      <many-to-many column="userId" class="tik2005.tik2005.user, tik2005" />       
    </bag>
             
</hibernate-mapping>


With the inverse="true" attribute, you are telling NHibernate to ignore this end of the association, and use the "User.Roles" collection to manage changes to the association.

If you really want to have NHibernate delete associations, you will probably need to refactor your code, create an association class, and use a one-to-many relationships. In that instance you can use cascade="all-delete-orphan" to delete the associations as you want.

HTH,

-devon


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 19, 2005 4:07 am 
Newbie

Joined: Wed May 18, 2005 8:56 am
Posts: 5
Location: tibro, sweden
Thank you :D Now it works like i want it to do


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