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)
);