NHibernate的许多一对多的关系问题:可以选择,无法更新 [英] NHibernate many-to-many relationship question: can select, can't update

查看:145
本文介绍了NHibernate的许多一对多的关系问题:可以选择,无法更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个用户都有角色的列表:

 <类名=用户为lazy =false的表= 用户> 
< ID名称=ID类型=整数>
<生成器类=本地/>
< / ID>

<属性名=姓名/>

<包名称=RoleList的表=USER_ROLE逆=真为lazy =false的集合类=角色>
<键列=用户ID外键=ID/>
<许多对多类=角色栏=角色ID/>
< /袋>

< /班>



我跟踪所有查询到SQL服务器。当我的选择的用户,我也看到了SQL SELECT语句(这是确定)。问题是,当我试图的更新的现有用户(即有角色):我只看到一个更新用户表,但不 USER_ROLE (这是坏的)



要澄清:我不指望在<$ C插入/更新$ C>角色表(但在 USER_ROLE ,因为角色是永久,并且可以附加和自由超脱。



为什么的选择的正常工作,但不是的更新的请如果需要的信息了 - ?只问 - 我会尽力回答。所有的二次问题



更新:角色映射:

 <类名=角色为lazy =false的表=角色> 
< ID名称= ID类型=整数>
<生成器类=本地/>
< / ID>

<属性名=姓名/ >
<属性名=说明/>
< /班>

更新N2:这是怎样的表的定义:



角色 表:

  CREATE TABLE [角色](
[ID] INTEGER NOT NULL PRIMARY KEY,
【名称】文本NOT NULL,
[LastChanged] INT NOT NULL DEFAULT(0)
);

创建UNIQUE INDEX ON uidxUserName角色(名称COLLATE NOCASE);



用户 表:

  CREATE TABLE [用户](
[ID] INTEGER NOT NULL PRIMARY KEY,
[名称】文本NOT NULL,
[LastChanged] INT NOT NULL DEFAULT(0)
);

创建唯一索引uidxRoleName上的用户(名称COLLATE NOCASE);



USER_ROLE relatioin表(外键):

  CREATE TABLE [USER_ROLE(
[用户ID] INTEGER NOT NULL,
[角色ID] INTEGER NOT NULL,
[LastChanged] INT NOT NULL DEFAULT(0),
PRIMARY KEY(用户ID,角色ID),
外键(用户ID)参考文献用户(ID) ,
外键(角色ID)参考角色(ID)
);


解决方案

我已经遇到了类似的问题我自己。



试着删除逆=真上的用户收集和角色集合,看看是否有任何两个区别。这为我工作。



有似乎与许多一对多的关系,并在属性的一些意外行为。也许别人可以解释为什么发生的比我好。 :)



更新::如果我没有记错的这里,你需要一个多到多袋的作用下映射映射。的你应该尽量让逆=FALSE或不属性在所有。的结果
你关于外键值使用有一定的?



另外,我映射时,认为类属性,则需要提供完全合格的名称,例如: Namespace.Class,大会



此外,它看起来大多像我自己的映射文件。


Each user has a list of roles:

  <class name="User" lazy="false" table="Users">
    <id name="Id" type="int">
      <generator class="native"/>
    </id>

    <property name="Name" />

    <bag name="RoleList" table="User_Role" inverse="true" lazy="false" collection-type="Roles">
      <key column="UserId" foreign-key="Id"/>
      <many-to-many class="Role" column="RoleId"/>
    </bag>

  </class>

I am tracking all the queries to the SQL server. When I select user, I also see the SQL SELECT statement (which is Ok). The problem is when I am trying to update an existing user (that has roles): I see only an update to User table, but not to User_Role (which is bad).

To clarify: I don't expect inserts/updates in Role table (but in User_Role, since the roles are "permanent" and can be attached and detached freely.

Why select works correctly, but not update. please? If anymore info is needed - just ask - I'll try to answer all the secondary questions.

Update: the Role mapping:

  <class name="Role" lazy="false" table="Roles">
    <id name="Id" type="int">
      <generator class="native"/>
    </id>

    <property name="Name" />
    <property name="Description" />
  </class>

Update N2: this is how the tables are defined:

Role table:

CREATE TABLE [Roles] (
[Id] INTEGER NOT NULL PRIMARY KEY,
[Name] text  NOT NULL,
[LastChanged] INT NOT NULL DEFAULT(0)
);

CREATE UNIQUE INDEX uidxUserName ON Roles (Name COLLATE NOCASE);

User table:

CREATE TABLE [Users] (
[Id] INTEGER NOT NULL PRIMARY KEY,
[Name] text NOT NULL,
[LastChanged] INT NOT NULL DEFAULT(0)
);

CREATE UNIQUE INDEX uidxRoleName ON Users (Name COLLATE NOCASE);

User_Role relatioin table (foreign keys):

CREATE TABLE [User_Role] (
[UserId] INTEGER NOT NULL,
[RoleId] INTEGER NOT NULL,
[LastChanged] INT NOT NULL DEFAULT(0),
PRIMARY KEY (UserId, RoleId),
FOREIGN KEY (UserId) REFERENCES Users(Id),
FOREIGN KEY (RoleId) REFERENCES Roles(Id)
);

解决方案

I've ran into a similar problem myself.

Try to remove the inverse="true" on both the User collection and the Role collection and see if there's any difference. That worked for me.

There seems some unexpected behavior with many-to-many relationships and the inverse attribute. Perhaps someone else can explain why that occurs better than me. :)

Update: If I'm not mistaken here, you'll need to map a many-to-many bag under the Role mapping. Try to keep both inverse="false" or without the inverse attribute at all.
Are you certain about the foreign-key value used there?

Also, I think when mapping the class attribute, you need to provide the fully qualified name, i.e: Namespace.Class, Assembly.

Besides that, it looks mostly like my own mapping files.

这篇关于NHibernate的许多一对多的关系问题:可以选择,无法更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆