nhibernate 多对多映射的数据更新-亚博电竞手机版

最近在用 nhibernate 做多对多更新时突然发现 nhibernate 更新的策略很差, 对多对多关系的更新居然是先全部删除再插入全部数据, 感觉非常奇怪, 现在还原如下:

原来的实体类关系如下:

public class user {     public virtual int id { get; set; }     public virtual string name { get; set; }     public virtual icollection roles { get; set; }     public user() {         roles = new hashset();     } }  public class role {     public virtual int id { get; set; }     public virtual string name { get; set; }     public virtual icollection users { get; set; }     public role() {         users = new hashset();     } }

即一个用户可以有多个角色, 一个角色也可以有多个人, 典型的多对多关系, 对应的映射代码如下:

public class usermapping : classmapping {     public usermapping() {         table("[user]");         id(m => m.id, map => {             map.column("[id]");             map.type(nhibernateutil.int32);             map.generator(generators.identity);         });         property(m => m.name, map => {             map.column("[name]");             map.type(nhibernateutil.string);         });         bag(             m => m.roles,             map => {                 map.table("[user_role]");                 map.key(k => { k.column("[userid]"); });             },             rel => {                 rel.manytomany(map => {                     map.class(typeof(role));                     map.column("[roleid]");                 });             }         );     } }  public class rolemapping : classmapping {     public rolemapping() {         table("[role]");         id(m => m.id, map => {             map.column("[id]");             map.type(nhibernateutil.int32);             map.generator(generators.identity);         });         property(m => m.name, map => {             map.column("[name]");             map.type(nhibernateutil.string);         });         bag(             m => m.users,             map => {                 map.table("[user_role]");                 map.key(k => { k.column("[roleid]"); });                 map.inverse(true);             },             rel => {                 rel.manytomany(map => {                     map.class(typeof(user));                     map.column("[userid]");                 });             }         );     } }

数据库关系图如下:

当向用户添加或删除角色是, 发现更新的效率特别低, 代码如下:

using (var session = sessionfactory.opensession()) {     var user = session.query().first();      var firstrole = user.roles.first();     user.roles.remove(firstrole);     session.update(user);      var rolecount = session.query().count();     var role = new role { name = "role "   (rolecount   1) };     session.save(role);      user.roles.add(role);     session.update(user);      session.update(user);     session.flush(); }

上面的代码是将用户的第一个角色删除, 再添加一个新的角色, nhibernate 生成的 sql 语句如下(仅包含对关系表 user_role 的操作):

delete from [user_role] where [userid] = @p0;@p0 = 1 [type: int32 (0)] insert into [user_role]  ([userid], [roleid]) values (@p0, @p1);@p0 = 1 [type: int32 (0)], @p1 = 2 [type: int32 (0)] insert into [user_role]  ([userid], [roleid]) values (@p0, @p1);@p0 = 1 [type: int32 (0)], @p1 = 7 [type: int32 (0)] insert into [user_role]  ([userid], [roleid]) values (@p0, @p1);@p0 = 1 [type: int32 (0)], @p1 = 6 [type: int32 (0)] insert into [user_role]  ([userid], [roleid]) values (@p0, @p1);@p0 = 1 [type: int32 (0)], @p1 = 10 [type: int32 (0)]

居然是先将属于该用户的全部角色删除, 再添加一份新的进来, 完全无法接受, 反过来思考觉得肯定是自己的问题, 经过一番搜索 (google), 发现 stackoverflow 上也有人问类似的问题, 并且最终在 nhibernate tip: use set for many-to-many associations 发现了亚博vip888的解决方案, 将多对多的映射的 bag 改为用 set , 问题终于得到了解决, 改过后的映射如下:

set(     m => m.roles,     map => {         map.table("[user_role]");         map.key(k => { k.column("[userid]"); });     },     rel => {         rel.manytomany(map => {             map.class(typeof(role));             map.column("[roleid]");         });     } );

将 usermapping 和 rolemapping 中多对多映射全部改为 set 之后, 上面的测试代码生成的 sql 如下:

delete from [user_role] where [userid] = @p0 and [roleid] = @p1;@p0 = 1 [type: int32 (0)], @p1 = 8 [type: int32 (0)] insert into [user_role]  ([userid], [roleid]) values (@p0, @p1);@p0 = 1 [type: int32 (0)], @p1 = 9 [type: int32 (0)]

在 nhibernate 参考文档的 19.5. understanding collection performance 中这样描述:

bags are the worst case. since a bag permits duplicate element values and has no index column, no primary key may be defined. nhibernate has no way of distinguishing between duplicate rows. nhibernate resolves this problem by completely removing (in a single delete) and recreating the collection whenever it changes. this might be very inefficient.

不只是多对多, 如果你的集合需要更新, nhibernate 推荐的是

19.5.2. lists, maps, idbags and sets are the most efficient collections to update

然而 bags 也不是一无是处:

19.5.3. bags and lists are the most efficient inverse collections

just before you ditch bags forever, there is a particular case in which bags (and also lists) are much more performant than sets. for a collection with inverse=”true” (the standard bidirectional one-to-many relationship idiom, for example) we can add elements to a bag or list without needing to initialize (fetch) the bag elements! this is because ilist.add() must always succeed for a bag or ilist (unlike an iset). this can make the following common code much faster.

parent p = sess.load(id); child c = new child(); c.parent = p; p.children.add(c);  //no need to fetch the collection! sess.flush();

由此可见, bag 在多对多映射更新时性能较差, 如果不需要更新,则可以放心使用, 在需要更新时则 set 是更好的选择。

展开全文
内容来源于互联网和用户投稿,文章中一旦含有亚博电竞手机版的联系方式务必识别真假,本站仅做信息展示不承担任何相关责任,如有侵权或涉及法律问题请联系亚博电竞手机版删除

最新文章

网站地图