C# NHibernate - 访问元素列表会引发错误(无法延迟初始化...)
我正在使用 MVC2、NHibernate 3.2。
我的类:
public class NHibernateHelper
{
private static ISessionFactory _sessionFactory;
private static ISessionFactory SessionFactory
{
get
{
if (_sessionFactory == null)
{
var configuration = new Configuration();
configuration.Configure();
configuration.AddAssembly(typeof(EstoqueEquipamento).Assembly);
_sessionFactory = configuration.BuildSessionFactory();
}
return _sessionFactory;
}
}
public static ISession OpenSession()
{
return SessionFactory.OpenSession();
}
}
我用以下命令查询元素:
public Car GetCar(object pk)
{
using (ISession session = NHibernateHelper.OpenSession())
{
return session.Get<Car>(pk);
}
}
汽车映射:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernateClasses" namespace="NHibernateClasses.Entities">
<class name="Car" table="STACK.CAR" lazy="true" dynamic-update="true" dynamic-insert="true" >
<id name="CarId" column="CAR_ID" type="Int32" unsaved-value="0">
<generator class="sequence">
<param name="sequence">SQ_CAR</param>
</generator>
</id>
<bag name="TireList" inverse="true" generic="true" lazy="true" >
<key>
<column name="CarId"/>
</key>
<one-to-many class="Tire" />
</bag>
<property name="Plate" type="String" column="MAH_PLATE" />
</class>
</hibernate-mapping>
轮胎映射:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernateClasses" namespace="NHibernateClasses.Entities">
<class name="Tire" table="STACK.Tire" lazy="true" dynamic-update="true" dynamic-insert="true" >
<id name="TireId" column="Tire_ID" type="Int32" unsaved-value="0">
<generator class="sequence">
<param name="sequence">SQ_Tire</param>
</generator>
</id>
<property name="Brand" type="String" column="MAH_BRAND" />
</class>
</hibernate-mapping>
然后我查询元素:
var car = GetCar(1);//works just fine
var tirelist = car.Tires;//throws error!
第一行没有问题,但最后一行抛出以下错误:
初始化[汽车] - 失败 延迟初始化角色集合: 汽车轮胎清单, 没有会话或会话已关闭
如果您还需要配置文件,请告诉我。
谢谢
I'm using MVC2, NHibernate 3.2.
My Classes:
public class NHibernateHelper
{
private static ISessionFactory _sessionFactory;
private static ISessionFactory SessionFactory
{
get
{
if (_sessionFactory == null)
{
var configuration = new Configuration();
configuration.Configure();
configuration.AddAssembly(typeof(EstoqueEquipamento).Assembly);
_sessionFactory = configuration.BuildSessionFactory();
}
return _sessionFactory;
}
}
public static ISession OpenSession()
{
return SessionFactory.OpenSession();
}
}
I query the element with this:
public Car GetCar(object pk)
{
using (ISession session = NHibernateHelper.OpenSession())
{
return session.Get<Car>(pk);
}
}
The Car mapping:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernateClasses" namespace="NHibernateClasses.Entities">
<class name="Car" table="STACK.CAR" lazy="true" dynamic-update="true" dynamic-insert="true" >
<id name="CarId" column="CAR_ID" type="Int32" unsaved-value="0">
<generator class="sequence">
<param name="sequence">SQ_CAR</param>
</generator>
</id>
<bag name="TireList" inverse="true" generic="true" lazy="true" >
<key>
<column name="CarId"/>
</key>
<one-to-many class="Tire" />
</bag>
<property name="Plate" type="String" column="MAH_PLATE" />
</class>
</hibernate-mapping>
The Tire mapping:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernateClasses" namespace="NHibernateClasses.Entities">
<class name="Tire" table="STACK.Tire" lazy="true" dynamic-update="true" dynamic-insert="true" >
<id name="TireId" column="Tire_ID" type="Int32" unsaved-value="0">
<generator class="sequence">
<param name="sequence">SQ_Tire</param>
</generator>
</id>
<property name="Brand" type="String" column="MAH_BRAND" />
</class>
</hibernate-mapping>
Then i query the element:
var car = GetCar(1);//works just fine
var tirelist = car.Tires;//throws error!
The first line works with no problems, but the last line throws the following error:
Initializing[Car]-failed
to lazily initialize a collection of role:
Car.TireList,
no session or session was closed
Let me know if you need the config file also.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 GetCar 中,您先关闭会话(由于
using
),然后再在代码中访问 Tires 集合。默认情况下,nHibernate 中集合是延迟加载的,因此请确保在结束事务之前获取集合。您可以使用预加载来确保 nHibernate 也加载子集合。
In GetCar you close the session (due to the
using
), before you access the Tires-collection later on in your code. Collections are loaded lazily by default in nHibernate, so make sure you fetch the collection before ending the transaction.You could use Eager loading to make sure nHibernate loads the child collection as well.