代码改变世界

NHibernate学习笔记(一)常见异常错误

2013-07-09 22:00  RobinShuai  阅读(247)  评论(0)    收藏  举报

一、映射配置导致的异常

  1. NHibernate.QueryException:could not resolve property
    在使用如下代码中的方法时抛出该异常:
    public IList<Entity.Order> GetOrders(Guid customerId,decimal amount)
    {
    using(ISession session = NHibernateHelper.GetSession())
    {
    IList
    <Entity.Order> orders = session.CreateCriteria<Entity.Order>()
    .Add(Restrictions.Eq(
    "Amount", amount))
    .Add(Restrictions.Eq(
    "CustomerId",customerId))
    .List
    <Entity.Order>();
    return orders;
    }
    }

    下面是正确的代码:

    public IList<Entity.Order> GetOrders(Guid customerId,decimal amount)
    {
    using(ISession session = NHibernateHelper.GetSession())
    {
    IList
    <Entity.Order> orders = session.CreateCriteria<Entity.Order>()
    .Add(Restrictions.Eq(
    "Amount", amount))
    .CreateCriteria(
    "Customer")
    .Add(Restrictions.Eq(
    "CustomerId",customerId))
    .List
    <Entity.Order>();
    return orders;
    }
    }