nhibernate之many-to-many的性能
NHibernate簡化了DLL,但降低了性能?
Northwind的Employee和Territories具有多對多的關系,關系存放在EmployeeTerritories表里。
所以在兩個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有一個記錄對應Territories的5條記錄,
EmployeeID? territoryid
15??????????????? ?01881
15??????????????? ?01882
15?????????????? ? 01883
15??????????????? ?01884
15??????????????? ?01885
現在要給這個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查看器查看,發現sql先delete了原來的EmployeeID=15的5條記錄,然后再insert這5條記錄,再insert新的id=01886的記錄,總共執行了7條sql語句
如果原先有上百條記錄,那不是要執行幾百條記錄了?NHibernate中有沒有什么配置可以改變?
轉載于:https://www.cnblogs.com/sunsolaris/archive/2008/10/10/1308256.html
總結
以上是生活随笔為你收集整理的nhibernate之many-to-many的性能的全部內容,希望文章能夠幫你解決所遇到的問題。