-->
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.  [ 1 post ] 
Author Message
 Post subject: M:N relationship loads correctly but won't save?
PostPosted: Thu Jul 31, 2008 11:56 pm 
Beginner
Beginner

Joined: Fri May 30, 2008 3:57 pm
Posts: 26
NHibernate 1.2
.Net 3.5

I'm at my wits end, I have no idea where to look next. I have a many to many relationship that just isn't saving correctly. The entity itself will save, but no entries are added to the association table for the relationship. If I manually add the information and load the object, it loads correctly.



User DDL
Code:
USE [sample]
GO
/****** Object:  Table [dbo].[Users]    Script Date: 07/31/2008 22:40:57 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Users](
   [id] [int] IDENTITY(1,1) NOT NULL,
   [associated_id] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
   [optimistic_lock] [smallint] NOT NULL,
CONSTRAINT [PK_users] PRIMARY KEY CLUSTERED
(
   [id] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]



Roles DDL

Code:
USE [sample]
GO
/****** Object:  Table [dbo].[Roles]    Script Date: 07/31/2008 22:41:40 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Roles](
   [id] [smallint] IDENTITY(1,1) NOT NULL,
   [name] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
   [privilege_type_id] [smallint] NOT NULL,
   [optimistic_lock] [smallint] NOT NULL,
CONSTRAINT [PK_Roles] PRIMARY KEY CLUSTERED
(
   [id] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]




Association Table
Code:
USE [sample]
GO
/****** Object:  Table [dbo].[Users_Roles]    Script Date: 07/31/2008 22:42:19 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Users_Roles](
   [user_id] [int] NOT NULL,
   [role_id] [int] NOT NULL,
   [sequence] [smallint] NOT NULL
) ON [PRIMARY]




User config
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="UserModel, User"
         optimistic-lock="version"
         table="Users">

    <id name="id" column="id" type="Int16" >
      <generator class="identity" />
    </id>
    <version name="optimisticLock" column="optimistic_lock" type="Int16" />

    <property name="associatedID" column="associated_id" type="String" length="50"/>

    <list name="roles" lazy="true" table="Users_Roles" cascade="none">
      <key column="user_id"/>
      <index column="sequence" type="Int16" />

      <many-to-many column="role_id" class="RoleModel, User" />
    </list>
   
  </class>
</hibernate-mapping>



Role config
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="RoleModel, User"
         optimistic-lock="version"
         table="Roles">

    <id name="id" column="id" type="Int16" >
      <generator class="identity" />
    </id>
    <version name="optimisticLock" column="optimistic_lock" type="Int16" />

    <property name="name" column="name" type="String" length="50"/>
    <many-to-one name="type" column="privilege_type_id" lazy="false" class="PrivilegeTypeModel, User" />
   
  </class>
</hibernate-mapping>





User model
Code:
internal class UserModel
   {
      private String _associatedID;
      private IList<RoleModel> _roles;
      public UserModel() { }

      public virtual Int16 id { get; set; }
      public virtual Int16 optimisticLock { get; set; }

      public virtual String associatedID
      {
         get{return this._associatedID;}
         set
         {
            this._associatedID = value;
         }
      }

      public virtual IList<RoleModel> roles
      {
         get { return this._roles; }
         set
         {
            this._roles = value;
         }
      }


Role Model
Code:
   internal class RoleModel
   {
      private String _name;
      public RoleModel() { }

      public virtual Int16 id { get; set; }
      public virtual Int16 optimisticLock { get; set; }

      public virtual String name
      {
         get{return this._name;}
         set
         {
            this._name = value;
         }
      }
   }



Code that saves.
Code:
UserModel um = new UserModel();
         um.associatedID = "I'm a user model!";

         // loadRoles covers an expression query that loads
         // all the roles with ID's corresponding to the input array
         um.roles = loadRoles(roleIDs);

         this.session.Save(um);



The SQL being output by NHibernate
Code:
INSERT INTO Users (optimistic_lock, associated_id) VALUES (@p0, @p1); select SCOPE_IDENTITY(); @p0 = '1', @p1 = 'I'm a user model!'



I've verified that loadRoles is returning role models successfully, so that isn't the issue.


I'm hoping there's some small configuration issue I'm not aware of? I've built several projects with NHibernate so far and never ran into this issue. Any and all help would be greatly appreciated.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.