希望有人能指出我正确的方向。我刚开始使用 nHibernate,对此有点困惑。它在 .Net Web 应用程序中运行。
基本上我有 2 类 - 优惠券和出版商。
作为测试,并确保 NH 设置正确,我访问了 PublisherRepository 并按其名称提取了一个发布者。这工作正常并报告成功。
IPublisherRepository repo = new PublisherRepository();
Response.Write(repo.GetByName("Publisher 5"));
作为第二个测试,然后我使用 CreateQuery 方法获取所有发布者,如下所示:
IQuery query = session.CreateQuery("from CartManData.Domain.Publisher pub");
这不返回任何数据 - 列表为空。使用 Linq 也是如此:
session.Query<Publisher>().Where(x=>x.Name == "Publisher 4").ToList<Publisher>()
使用 Sql Profiler,我可以看到第一个测试命中数据库,并在延迟加载关闭时检索集合(称为 Coupons 属于发布者)。然而,后两种方法根本没有触及数据库——我不知道为什么。
这是 Publisher 和 Coupon 的映射文件。它们是嵌入的,我知道它们正在工作,否则 repo 也不会工作:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="CartManData"
namespace="CartManData.Domain">
<class name="Publisher" lazy="false">
<id name="Id">
<generator class="guid"></generator>
</id>
<property name="Name"></property>
<property name="AddressLine1"></property>
<property name="AddressLine2"></property>
<property name="AddressLine3"></property>
<property name="Town"></property>
<property name="PostCode"></property>
<property name="Telephone"></property>
<property name="Email"></property>
<property name="Enabled"></property>
<property name="CommissionRate"></property>
<set name="Coupons" cascade="none" lazy="false">
<key column="PublisherId" ></key>
<one-to-many class="Coupon" />
</set>
</class>
</hibernate-mapping>
和优惠券:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="CartManData"
namespace="CartManData.Domain">
<class name="Coupon">
<id name="Id">
<generator class="guid"></generator>
</id>
<property name="Name"></property>
<property name="EffectiveFrom"></property>
<property name="EffectiveTo"></property>
<property name="UnitPrice"></property>
<property name="OriginalPrice"></property>
<property name="CouponImage"></property>
<property name="Enabled"></property>
<property name="PublisherId" not-null="false"></property>
</class>
</hibernate-mapping>
对此的任何帮助真的很感激 - 确定这是我错过的东西。
干杯,托尼
附加信息
Session 对象通过 HttpModule 检索,其中会话对象绑定到 CurrentSessionContext。这似乎工作正常,就像您检查会话是否打开一样,它报告它是。
PublisherRepository.GetByName() 看起来像这样:
using (ISession session = NHibernateHelper.OpenSession())
{
return session.CreateCriteria(typeof(Publisher))
.Add(NHibernate.Criterion.Restrictions.Eq("Name", name))
.UniqueResult<Publisher>();
}
Log4Net 输出
在通过 CreateQuery 调用期间(上面的第二个示例),这是 NHibernate 报告的内容:
2012-08-22 16:22:28,075 [15] DEBUG rollingFile - START of retrieval
2012-08-22 16:22:28,081 [15] DEBUG NHibernate.Engine.Query.QueryPlanCache - unable to locate HQL query plan in cache; generating (from CartManData.Domain.Publisher pub)
2012-08-22 16:22:28,128 [15] DEBUG NHibernate.Hql.Ast.ANTLR.HqlParseEngine - parse() - HQL: from CartManData.Domain.Publisher pub
2012-08-22 16:22:28,174 [15] DEBUG NHibernate.Hql.Ast.ANTLR.ErrorCounter - throwQueryException() : no errors
2012-08-22 16:22:28,200 [15] DEBUG NHibernate.Engine.Query.QueryPlanCache - unable to locate HQL query plan in cache; generating (from CartManData.Domain.Publisher pub)
2012-08-22 16:22:28,201 [15] DEBUG NHibernate.Hql.Ast.ANTLR.HqlParseEngine - parse() - HQL: from CartManData.Domain.Publisher pub
2012-08-22 16:22:28,202 [15] DEBUG NHibernate.Hql.Ast.ANTLR.ErrorCounter - throwQueryException() : no errors
2012-08-22 16:22:28,206 [15] DEBUG NHibernate.Engine.Query.HQLQueryPlan - enumerable: from CartManData.Domain.Publisher pub
2012-08-22 16:22:28,208 [15] DEBUG NHibernate.Engine.QueryParameters - named parameters: {}
2012-08-22 16:22:28,210 [15] DEBUG rollingFile - End of retrieval