This worked for me.
Code:
<class xmlns="urn:nhibernate-mapping-2.2" name="User" table="HibernateUsers" >
<id name="UserId" column="ID_USER" type="Int32" >
<generator class="native"/>
</id>
<map table="USERGRADES" name="Grades">
<key column="ID_USER" />
<index-many-to-many class="Assignment" column="ID_ASSIGNMENT"/>
<element type="double" column="USER_GRADE" />
</map>
</class>
and this c# code:
Code:
Configuration cfg = new Configuration();
cfg.Configure();
cfg.AddAssembly("frmNhibernateTest");
ISessionFactory sf = cfg.BuildSessionFactory();
new SchemaExport(cfg).Create(true, true);
session = sf.OpenSession();
User user = new User();
session.Save(user);
Assignment ass = new Assignment();
ass.Name = "Hello World";
session.Save(ass);
user.Grades.Add(ass, 4.5);
session.Save(user);
session.Flush();
Assignment just consists of an Id and a Name for simplicity.
Maybe you should try to save the User Object before adding something to the Dictionary.
I also added the complete SQL statements executed by NHibernate:
Code:
create table HibernateUsers (
ID_USER INT IDENTITY NOT NULL,
primary key (ID_USER)
)
create table USERGRADES (
ID_USER INT not null,
USER_GRADE DOUBLE PRECISION null,
ID_ASSIGNMENT INT not null,
primary key (ID_USER, ID_ASSIGNMENT)
)
create table ASSIGNMENT (
ID_ASSIGNMENT INT IDENTITY NOT NULL,
NAME NVARCHAR(255) null,
primary key (ID_ASSIGNMENT)
)
alter table USERGRADES add constraint FK40D019BBC5BBBBB4 foreign key (ID_USER) references HibernateUsers
alter table USERGRADES add constraint FK40D019BBF3042FE8 foreign key (ID_ASSIGNMENT) references ASSIGNMENT
NHibernate: INSERT INTO HibernateUsers DEFAULT VALUES; select SCOPE_IDENTITY()
NHibernate: INSERT INTO ASSIGNMENT (NAME) VALUES (@p0); select SCOPE_IDENTITY(); @p0 = 'Hello World'
NHibernate: INSERT INTO USERGRADES (ID_USER, ID_ASSIGNMENT, USER_GRADE) VALUES (@p0, @p1, @p2); @p0 = '1', @p1 = '1', @p2 = '4,5'