我有两个实体密码和密码哈希。Password 有一组 PasswordHashes(这些是具有不同掩码的哈希)。
当我删除密码时,我希望所有关联的 PasswordHash 也被删除。
我做了这样的映射:
mapping.HasMany(x => x.PasswordHashes)
.Cascade
.All()
.Inverse()
.ReferencedBy(x => x.Password);
我希望生成两个查询:
DELETE FROM "PasswordHash" WHERE "IdPassword" = :p0
显然
DELETE FROM "Password" WHERE "IdPassword" = :p0
相反,NHibernate 生成了 N+1 个查询,如下所示:
DELETE FROM "PasswordHash" WHERE "IdPasswordHash" = :p0 (N)
和
DELETE FROM "Password" WHERE "IdPassword" = :p0 (1)
如何将这种行为更改为更优化?
编辑:删除代码:
session.Delete(password);
session.Flush();