【发布时间】:2010-05-12 20:02:02
【问题描述】:
我正在检索与其他表/实体有关系的休眠实体列表。我注意到 NHibernate 不是执行 JOINS 并填充属性,而是检索实体,然后为每个属性调用一个选择。例如,如果一个用户可以有多个角色,而我从数据库中检索一个用户,Nhibernate 会检索该用户,然后使用另一个 select 语句填充角色。问题是我想检索哦,比如说一个产品列表,这些产品具有各种多对多关系以及与具有自己关系的项目的关系。最后,我需要通过一千多个数据库调用来检索 30 种产品的列表。
我还将默认延迟加载设置为 false,因为每当我将实体列表保存到会话时,尝试在另一个页面上检索它时都会收到错误:LazyInitializationException: 无法初始化代理
如果有人能提供任何启示,我将不胜感激。
这是和示例 sql:
SELECT *
FROM Product b
LEFT JOIN Minisites mini
ON b.MinisiteId = mini.MinisiteId
LEFT JOIN TimeRanges tr
ON mini.TimeRange = tr.TimeRangeId
WHERE mini.MinisiteId = 7
OR tr.FromDate > '3/18/2010 12:00:00 AM'
ORDER BY CASE
WHEN mini.MinisiteId = 7 then 1
else 0
end
+ CASE
WHEN tr.FromDate > '3/18/2010 12:00:00 AM' then 1
else 0
end DESC
这是我调用来检索产品的代码(我传递了一个示例产品来创建查询,创建查询的实际代码太长而无法发布,但查询字符串基本上是我上面发布的):
Session.CreateSQLQuery(GetSqlQuery(product)).AddEntity(typeof(Product)).SetResultTransformer(new DistinctRootEntityResultTransformer()).UniqueResult().List<Product>();
我从该函数返回绑定到数据列表的产品列表。数据列表包含名称、公司、州等行,当您单击它时,它会打开下面的框,其中包含所有产品信息、清晰度、切割等。每个产品都与其他属性相关,这些属性与其他属性相关,例如,它与用户相关,与地址相关,与国家相关,等等。对于这些属性中的每一个,都会进行选择声明。
这是我的产品 hbm 文件:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="IDI.Domain" namespace="IDI.Domain.Entities" default-lazy="false">
<class name="IDI.Domain.Entities.Product,IDI.Domain" table="Product">
<id name="ProductId" column="ProductId" type="Int32">
<generator class="native"></generator>
</id>
<set name="Specialty" generic="true" table="ProductInSpecialties" cascade="save-update" lazy="false">
<key column="ProductId" not-null="true">
</key>
<many-to-many class="IDI.Domain.Entities.Specialty,IDI.Domain" column="SpecialtyId" lazy="false"> </many-to-many>
</set>
<!--Search By Rounds -->
<set name="Clarity" generic="true" table="ProductInClarity" lazy="false" >
<key column="ProductId" not-null="true">
</key>
<many-to-many class="IDI.Domain.Entities.Clarity,IDI.Domain" column="ClarityId" lazy="false" > </many-to-many>
</set>
<set name="Color" generic="true" table="ProductInColor">
<key column="ProductId" not-null="true">
</key>
<many-to-many class="IDI.Domain.Entities.Color,IDI.Domain" column="ColorId" lazy="false"> </many-to-many>
</set>
<set name="Carat" generic="true" table="ProductInCarat">
<key column="ProductId" not-null="true">
</key>
<many-to-many class="IDI.Domain.Entities.Carat,IDI.Domain" column="CaratId" lazy="false"> </many-to-many>
</set>
<set name="Finish" generic="true" table="ProductInFinish" >
<key column="ProductId" not-null="true">
</key>
<many-to-many class="IDI.Domain.Entities.Finish,IDI.Domain" column="FinishId" lazy="false"> </many-to-many>
</set>
<!--End Search By Rounds -->
<!--Search By Fancy Cut -->
<set name="FCShape" generic="true" table="ProductInShapes">
<key column="ProductId" not-null="true">
</key>
<many-to-many class="IDI.Domain.Entities.Shape,IDI.Domain" column="ShapeId" lazy="false"> </many-to-many>
</set>
<set name="FCClarity" generic="true" table="ProductInFCClarity">
<key column="ProductId" not-null="true">
</key>
<many-to-many class="IDI.Domain.Entities.Clarity,IDI.Domain" column="ClarityId" lazy="false"> </many-to-many>
</set>
<set name="FCColor" generic="true" table="ProductInFCColor">
<key column="ProductId" not-null="true">
</key>
<many-to-many class="IDI.Domain.Entities.Color,IDI.Domain" column="ColorId" lazy="false"> </many-to-many>
</set>
<set name="FCCarat" generic="true" table="ProductInFCCarat">
<key column="ProductId" not-null="true">
</key>
<many-to-many class="IDI.Domain.Entities.Carat,IDI.Domain" column="CaratId" lazy="false" > </many-to-many>
</set>
<set name="FCFinish" generic="true" table="ProductInFCFinish">
<key column="ProductId" not-null="true">
</key>
<many-to-many class="IDI.Domain.Entities.Finish,IDI.Domain" column="FinishId" lazy="false" > </many-to-many>
</set>
<!--End Search By Fancy Cut -->
<set name="TurnOver" generic="true" table="ProductInTurnOver" lazy="false">
<key column="ProductId" not-null="true">
</key>
<many-to-many class="IDI.Domain.Entities.TurnOver,IDI.Domain" column="TurnOverId" lazy="false"> </many-to-many>
</set>
<many-to-one name="User" column="UserId" not-null="true" cascade="save-update" lazy="false" class="IDI.Domain.Entities.BursaUser,IDI.Domain"></many-to-one>
<many-to-one name="Minisite" column="MinisiteId" not-null="false" lazy="false" class="IDI.Domain.Entities.Minisite,IDI.Domain"></many-to-one>
</class>
</hibernate-mapping>
谢谢。
艾坦
【问题讨论】:
标签: database nhibernate optimization