How make mapping to improve the speed of application?
I have a problem with my mappings Employee and Project: For example in Employee mapping I have "bag" to map other tables. The problem is when I open the Employee window, this take a lot of time(10 sec) opening the Window, how can I make the mapping better and also faster? Maybe in the lazy or in the fetch?.
This is the mapping for Employee:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="AdminProject"
namespace="AdminProject.Business.Entity">
<class name="Employee">
<id name="EmployeeId" ty开发者_运维技巧pe="int">
</id>
<property name="OperatorNum"
generated="always"
update="false"
insert="false"
type="int"/>
<property name="Password" type="string"/>
<property name="Name" type="string"/>
<property name="LastName" type="string"/>
<property name="DateBegin" type="DateTime"/>
<property name="DateEnd" type="DateTime"/>
<property name="Telephone" type="string"/>
<property name="Address" type="string"/>
<many-to-one
name="EmployeeState"
column="EmployeeStateId"
class="EmployeeState"
fetch="join"/>
<bag name="EmployeebyProject" lazy="false">
<key column="EmployeeId"/>
<one-to-many class="EmployeebyProject"/>
</bag>
<bag name="EmployeeComments" lazy="false">
<key column="EmployeeId"/>
<one-to-many class="EmployeeComments"/>
</bag>
</class>
</hibernate-mapping>
Thanks..
You should use SQL Server Profiler to figure out what is being loaded from your database. Alternatively you can set NHibernate to log SQL. This way it will be easier for you to see what causes the delay. Very likely that it is caused by eagerly loading collections (your mapping has lazy="false"
). If this is the case you can simply set it to true (default).
<bag name="EmployeebyProject" lazy="true">
<key column="EmployeeId"/>
<one-to-many class="EmployeebyProject"/>
</bag>
<bag name="EmployeeComments" lazy="true">
<key column="EmployeeId"/>
<one-to-many class="EmployeeComments"/>
</bag>
Your many-to-one association is also loaded eagerly (fetch="join"
).
One of the popular approaches is to have all associations lazy (Lazy Default Fetch Plan). And then use eager loading in places where you are sure you will need association loaded. This however depends on your session management because lazy loading will not work if session is no longer available. There is a very good description of fetch plans and strategies in this book.
For your bag mapping, remove lazy="false". It's lazy by default.
精彩评论