我的一个POCO中有Status
属性作为鉴别器(非常类似于使用enum
)。
以下是具有Status
列的HBM的一部分。
<many-to-one class="Locate.Common.Domain.Statuses.Status, Locate.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="Status">
<column name="Status_id" not-null="true" />
</many-to-one>
这是Status
表的HBM。
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class xmlns="urn:nhibernate-mapping-2.2" name="Locate.Common.Domain.Statuses.Status, Locate.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`Status`">
<id access="nosetter.lowercase-underscore" name="Id" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Status_id" />
<generator class="assigned" />
</id>
<discriminator type="String">
<column name="Status_id" />
</discriminator>
<property access="nosetter.lowercase-underscore" name="Name" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Name" not-null="true" />
</property>
<property access="nosetter.camelcase-underscore" name="IsUserSelectable" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="IsUserSelectable" not-null="true" />
</property>
<subclass name="Locate.Common.Domain.Statuses.CallbackStatus, Locate.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" discriminator-value="3" />
<subclass name="Locate.Common.Domain.Statuses.EmergencyStatus, Locate.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" discriminator-value="5" />
<subclass name="Locate.Common.Domain.Statuses.FirstCheckerStatus, Locate.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" discriminator-value="1" />
<subclass name="Locate.Common.Domain.Statuses.PleaseAdviseStatus, Locate.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" discriminator-value="4" />
<subclass name="Locate.Common.Domain.Statuses.SecondCheckerStatus, Locate.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" discriminator-value="2" />
</class>
</hibernate-mapping>
最后,这是我的QueryOver
。
var locates =
session.QueryOver(() => locateAlias)
.SelectList(x => x
.Select(xx => xx.WorkToBeginDateTime).WithAlias(() => sentTo.DueDate)
.Select(xx => xx.SendTo).WithAlias(() => sentTo.SentTo)
.Select(xx => xx.TicketNo).WithAlias(() => sentTo.TicketNo)
.Select(xx => xx.Status).WithAlias(() => sentTo.Status)
)
.JoinAlias(() => locateAlias.Status, () => statusAlias)
.Where(() => locateAlias.IsComplete == false)
.TransformUsing(Transformers.AliasToBean<SentToDto>()).List<SentToDto>();
此查询运行时没有错误,但Status
属性未正确初始化。当我使用Status
值时,我收到此异常。
Initializing[Locate.Common.Domain.Statuses.Status#2]-Could not initialize proxy - no Session.
查看NHibernate Profiler我可以看到它实际上选择了正确的Status_id
。
HBM或查询有什么问题?
请注意,我已成功查询所有状态并接收所有状态鉴别器对象的列表。我还可以看到生成的代理对象包含Status
对象的正确值,因此NHibernate正确返回正确的值。
答案 0 :(得分:3)
您的查询并不是Eeger-fetch Status属性,稍后当您访问Status时,您已加载该位置的会话已经关闭。因此,而不是.JoinAlias(() => locateAlias.Status, () => statusAlias)
使用.Fetch(locate => locate.Status).Eager
编辑:我验证了它,因为选择状态不是急切获取的。如果返回locate-object,它只会急切地获取。你可以这样做:
var locates =
session.QueryOver(() => locateAlias)
.Where(() => locateAlias.IsComplete == false)
.Fetch(locate => locate.Status).Eager
.ToEnumerable()
.Select(locate => new SentToDto
{
DueDate = locate.WorkToBeginDateTime,
SendTo = locate.SendTo,
TicketNo = locate.TicketNo,
Status = locate.Status,
}.ToList();