我有一個模式,它看起來像這樣數據保存到表沒有主鍵
用戶
Id INT
Username VARCHAR(100)
Password VARCHAR(100)
User_Info_Key
Id INT
Description VARCHAR(100)
USER_INFO
User INT
Info_Key INT
Info_Value VARCHAR(100)
PRIMARY KEY(User, Info_Key)
這裏是我的用戶映射文件:
<class name="User" table="[user]">
<id name="Id">
<column name="id" />
<generator class="native" />
</id>
<property name="Username" />
<property name="Password" />
<bag name="InfoValues" cascade="all" lazy="false">
<key column="[user]" />
<one-to-many class="FMInfoValue" />
</bag>
</class>
<class name="InfoKey" table="[user_info_key]">
<id name="Id">
<column name="id" />
<generator class="native" />
</id>
<property name="Description" />
</class>
<class name="InfoValue" table="[user_info]">
<composite-id>
<key-property name="User" column="[user]" type="int"></key-property>
<key-property name="Key" column="[info_key]" type="int"></key-property>
</composite-id>
<property name="Value" column="[info_value]" />
<many-to-one name="FMInfoKey" class="FMInfoKey"
column="[info_key]"
cascade="all"
lazy="false" />
</class>
當我創建一個新的用戶我這樣做:
User newuser = new User();
newuser.Username = this.Username;
newuser.Password = this.Password;
session.Save(newuser);
而且這樣節省的基本信息。
但是當我嘗試添加的第一個名字(信息鍵值1)和姓(信息鍵值2)沒有被保存到數據庫:
InfoValue userfname = new InfoValue();
InfoKey userfnamekey = new InfoKey();
userfnamekey.Id = 1;
userfname.InfoKey = userfnamekey;
userfname.Key = 1;
userfname.User = (int) newuser.Id;
userfname.Value = this.Firstname;
session.Save(userfname);
InfoValue userlname = new InfoValue();
InfoKey userlnamekey = new InfoKey();
userlnamekey.Id = 2;
userlname.InfoKey = userlnamekey;
userlname.User = (int)newuser.Id;
userlname.Value = this.Lastname;
session.Save(userlname);
如何讓NHibernate的保存信息值表中有兩個信息值?
如果您發佈代碼,XML或數據樣本,**請**在文本編輯器中突出顯示這些行,然後單擊編輯器工具欄上的「代碼示例」按鈕(「{}」)以精確地設置格式和語法突出顯示它! – 2011-03-18 23:02:29