在Web App中使用IQuery和Repository的不同结果

时间:2012-08-22 12:16:17

标签: nhibernate

希望有人能指出我正确的方向。我刚刚开始使用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我可以看到第一个测试命中数据库,并在延迟加载关闭时检索该组(称为属于发布者的优惠券)。然而,后两种方法根本没有打到数据库 - 我很难过为什么。

这是Publisher和优惠券的映射文件。他们是嵌入式的,我知道他们正在工作,否则回购也不会有效:

<?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>

对此的任何帮助都非常感谢 - 确定这是我错过的。

干杯, 贝

其他信息

通过HttpModule检索Session对象,其中会话对象绑定到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

1 个答案:

答案 0 :(得分:1)

您的映射似乎有问题 我试图重新创建你的情况,我已经改变了你的hbm文件

<强>发布商

<?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="all-delete-orphan" inverse="true" lazy="false">
            <key column="PublisherId" />
            <one-to-many class="CartManData.Domain.Coupon, CartManData" />
        </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>

        <many-to-one class="CartManData.Domain.Publisher, CartManData" name="Publisher">
            <column name="PublisherId" not-null="true" />
        </many-to-one>

    </class>
</hibernate-mapping>

正如您所看到的,我在优惠券上使用了多对一关系。
您可以阅读这些关系的工作原理here 我在为Publisher映射定义的集合中使用了inverse =“true”。有关inverse的更多信息。

如果你想看看这两个类:

public class Publisher
{
    public Publisher()
    {
        this.Coupons = new HashSet<Coupon>();
    }

    public virtual Guid Id { get; set; }
    public virtual string Name { get; set; }
    public virtual string AddressLine1 { get; set; }
    public virtual string AddressLine2 { get; set; }
    public virtual string AddressLine3 { get; set; }
    public virtual string Town { get; set; }
    public virtual string PostCode { get; set; }
    public virtual string Telephone { get; set; }
    public virtual string Email { get; set; }
    public virtual bool Enabled { get; set; }
    public virtual decimal CommissionRate { get; set; }

    public virtual ICollection<Coupon> Coupons { get; set; }
}

public class Coupon
{
    public virtual Guid Id { get; set; }
    public virtual string Name { get; set; }
    public virtual DateTime EffectiveFrom { get; set; }
    public virtual DateTime EffectiveTo { get; set; }
    public virtual double UnitPrice { get; set; }
    public virtual double OriginalPrice { get; set; }
    public virtual string CouponImage { get; set; }
    public virtual bool Enabled { get; set; }

    public virtual Publisher Publisher { get; set; }

}

您可以下载一个工作示例( NHVariousTests here