I have a problem using the idbag. In my database I have a User table and a Role table and between them a many-to-many join relationship table UserRole. My User class has a list of roles, and my Role class has a list of users. From my configuration:
USER:
<hibernate-mapping xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:nhibernate-mapping-2.2"> <class name="Business.Domain.User,Business" table="User" lazy="true" optimistic-lock="version" dynamic-update="true" dynamic-insert="true" > <cache usage="read-write" /> <id name="Guid" column="[UserGUID]" type="Guid"> <generator class="assigned" /> </id>
<version name="RowVersion" />
<property column="[AccountLockoutFlag]" type="Boolean" name="AccountLockoutFlag" not-null="true" /> <property column="[ActiveFlag]" type="Boolean" name="ActiveFlag" not-null="true" /> <property column="[CanSeeMeFlag]" type="Boolean" name="CanSeeMeFlag" not-null="true" /> <property column="[DomainIdentity]" type="String" name="DomainIdentity" length="256" /> <property column="[HasCustomizedHomePageFlag]" type="Boolean" name="HasCustomizedHomePageFlag" /> <property column="[Password]" type="String" name="EncryptedPassword" not-null="true" length="256" /> <property column="[PasswordExpirationDate]" type="timestamp" name="PasswordExpirationDate" /> <property column="[UserName]" type="String" name="Name" not-null="true" length="256" /> <property column="[UserPreferences]" type="String" name="Preferences" length="2147483647" />
<idbag name="ListOfRole" lazy="true" inverse="true" collection-type="Business.Domain.RolePersistentIdListType,Business" table="UserRole" cascade="save-update"> <collection-id column="UserRoleGUID" type="Guid"> <generator class="guid" /> </collection-id> <key column="UserGUID"/> <many-to-many class="Business.Domain.Role,Business" column="RoleGUID" /> </idbag> </class> </hibernate-mapping>
ROLE:
<hibernate-mapping xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:nhibernate-mapping-2.2"> <class name="Business.Domain.Role,Business" table="Role" lazy="true" optimistic-lock="version" dynamic-update="true" dynamic-insert="true" > <cache usage="read-write" /> <id name="Guid" column="[RoleGUID]" type="Guid"> <generator class="assigned" /> </id>
<version name="RowVersion" />
<property column="[Comments]" type="String" name="Comments" length="2147483647" /> <property column="[RoleName]" type="String" name="Name" not-null="true" length="50" /> <idbag name="ListOfUser" lazy="true" inverse="false" collection-type="Business.Domain.UserPersistentIdListType,Business" table="UserRole" cascade="save-update"> <collection-id column="UserRoleGUID" type="Guid"> <generator class="guid" /> </collection-id> <key column="RoleGUID"/> <many-to-many class="Business.Domain.User,Business" column="UserGUID" /> </idbag> </class> </hibernate-mapping>
When I add a role to a user, and call ISession.Save, the version column RowVersion is set to the same value it is in the database. However, after a call to ISession.Flush, the version column has been changed for the Role, and looking in my database I see that an update statement has been executed against my Role table where the only thing that changed is the version column.
Is this the desired behavior? Can someone explain to me if there is a way I can avoid having this happen? The Role object is not changing at all, and it should not be updating.
|