nhibernate之many-to-many的性能
NHibernate簡(jiǎn)化了DLL,但降低了性能?
Northwind的Employee和Territories具有多對(duì)多的關(guān)系,關(guān)系存放在EmployeeTerritories表里。
所以在兩個(gè)Entity建立many-to-many。
Employees.hbm.xml:
??<bag name="EmployeeTerritories" table="EmployeeTerritories" inverse="false" lazy="true" cascade="all-delete-orphan">
???<key>
????<column name="EmployeeID" length="4" sql-type="int" not-null="true"/>
???</key>
???<many-to-many class="DataEntity.Territory, DataEntity">?
????<column name="TerritoryID" length="20" sql-type="nvarchar" not-null="true"/>
???</many-to-many>
??</bag>???
Territories.hbm.xml:?
??? <bag name="TerritoryEmployees" table="EmployeeTerritories" inverse="true" lazy="true" cascade="all-delete-orphan">
????? <key>
??????? <column name="TerritoryID" length="20" sql-type="nvarchar" not-null="true"/>
????? </key>
????? <many-to-many class="DataEntity.Employee, DataEntity">
??????? <column name="EmployeeID" length="4" sql-type="int" not-null="true"/>
????? </many-to-many>
??</bag>
在EmployeeTerritories里,原先Employee有一個(gè)記錄對(duì)應(yīng)Territories的5條記錄,
EmployeeID? territoryid
15??????????????? ?01881
15??????????????? ?01882
15?????????????? ? 01883
15??????????????? ?01884
15??????????????? ?01885
現(xiàn)在要給這個(gè)EmployeeID=15的記錄添加新的Territories記錄(territoryid=01886)
??????????? ISession session = SessionFactory.OpenSession(_AssemblyName);
??????????? Employee employee = null;
??????????? Territory territory = null;
??????????? //try...
??????????? //catch...
??????????? //finaly...
??????????? ITransaction transaction = session.BeginTransaction();
??????????? territory = (Territory)session.Get(typeof(Territory), "01886");
??????????? transaction.Commit();
??????????? transaction = session.BeginTransaction();
??????????? employee = (Employee)session.Get(typeof(Employee), 15);
??????????? employee.EmployeeTerritories.Add(territory);
??????????? session.Update(employee);
??????????? transaction.Commit();
用sql查看器查看,發(fā)現(xiàn)sql先delete了原來(lái)的EmployeeID=15的5條記錄,然后再insert這5條記錄,再insert新的id=01886的記錄,總共執(zhí)行了7條sql語(yǔ)句
如果原先有上百條記錄,那不是要執(zhí)行幾百條記錄了?NHibernate中有沒有什么配置可以改變?
轉(zhuǎn)載于:https://www.cnblogs.com/sunsolaris/archive/2008/10/10/1308256.html
總結(jié)
以上是生活随笔為你收集整理的nhibernate之many-to-many的性能的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。