0

我一直在尝试使用 HQL 获得两个类的结果。它适用于一张桌子,但不适用于两张桌子。

下面的两个实体;

public class A_CUSTOMER
    {
        public virtual string CUSTOMER_NO { get; set; }
        public virtual string ADDRESS_LINE { get; set; }

        public virtual IList<A_ACCOUNT> ACCOUNTS { get; set; }
    }

public class A_ACCOUNT
    {
        public virtual string AC_NO { get; set; }
        public virtual string CUST_NO { get; set; }
        public virtual string CCY { get; set; } 
        public virtual string AVL_BAL { get; set; }  


        public virtual A_CUSTOMER CUSTOMER { get; set; }

    }

以下类的映射;

public class A_CUSTOMERMap:ClassMap<A_CUSTOMER>
    {
        public A_CUSTOMERMap()
        {
            Id(x => x.CUSTOMER_NO);
            Map(x => x.ADDRESS_LINE);

            HasMany(x => x.ACCOUNTS).KeyColumns.Add("CUST_NO");
        }
    }


public class A_CUST_ACCOUNTMap:ClassMap<A_CUST_ACCOUNT>
    {
        public A_CUST_ACCOUNTMap()
        {
            Id(x => x.AC_NO);
                Map(x=>x.CUST_NO);
                Map(x => x.AVL_BAL);
                Map(x => x.CCY);
                References(x => x.CUSTOMER,"CUST_NO");

        }
    }

现在,我的控制器中有这样的代码:

string myString = "1001,1002,1003,1004,1005";
var indNumbers = myString.Split(',');
string qry = @"select h.AC_NO,h.BRANCH_CODE,h.CUST_NO,h.CCY,h.AVL_BAL ,g.ADDRESS_LINE"+
                "from A_ACCOUNT as h,A_CUSTOMER g "+ 
                "where h.CUST_NO=g.CUSTOMER_NO AND h.AC_NO IN (:list)";
                ;
res = session.CreateQuery(qry)
      .SetParameterList("list", indNumbers)
      .List();

我也尝试过不同的查询字符串:

string qry = @"select h.AC_NO,h.BRANCH_CODE,h.CUST_NO,h.CCY,h.AVL_BAL ,g.ADDRESS_LINE"+
               "from A_ACCOUNT as h left join A_CUSTOMER g on "+
               "h.CUST_NO=g.CUSTOMER_NO"+ 
               "where  h.AC_NO IN (:list)";

下面遇到的错误,任何帮助将不胜感激。

NHibernate.Hql.Ast.ANTLR.QuerySyntaxException was unhandled by user code
  HResult=-2146232832
  Message=Exception of type 'Antlr.Runtime.NoViableAltException' was thrown. near line 1, column 83
  Source=NHibernate
  StackTrace:
       at NHibernate.Hql.Ast.ANTLR.ErrorCounter.ThrowQueryException()
       at NHibernate.Hql.Ast.ANTLR.HqlParseEngine.Parse()
       at NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(String queryString, String collectionRole, Boolean shallow, IDictionary`2 filters, ISessionFactoryImplementor factory)
       at NHibernate.Engine.Query.HQLStringQueryPlan.CreateTranslators(String hql, String collectionRole, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory)
       at NHibernate.Engine.Query.HQLStringQueryPlan..ctor(String hql, String collectionRole, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory)
       at NHibernate.Engine.Query.HQLStringQueryPlan..ctor(String hql, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory)
       at NHibernate.Engine.Query.QueryPlanCache.GetHQLQueryPlan(String queryString, Boolean shallow, IDictionary`2 enabledFilters)
       at NHibernate.Impl.AbstractSessionImpl.GetHQLQueryPlan(String query, Boolean shallow)
       at NHibernate.Impl.AbstractSessionImpl.CreateQuery(String queryString)
       at MvcApplication2.Controllers.CustAccountController.GetCustData(jQueryDataTableParamModel param) in ...
       at lambda_method(Closure , ControllerBase , Object[] )
       at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
       at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41()
       at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _)
       at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33()
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49()
  InnerException: 
4

2 回答 2

1

您在单词之后ADDRESS_LINE和第二行之前缺少空格from

string qry = @"select h.AC_NO,h.BRANCH_CODE,h.CUST_NO,h.CCY,h.AVL_BAL ,g.ADDRESS_LINE"+
                    "from A_ACCOUNT as h,A_CUSTOMER g "+ 
                    "where h.CUST_NO=g.CUSTOMER_NO AND h.AC_NO IN (:list)";

重述这一点的一种方法是删除所有出现的引号:-" + "例如

var qry = @"select h.AC_NO,h.BRANCH_CODE,h.CUST_NO,h.CCY,h.AVL_BAL ,g.ADDRESS_LINE  
                           from A_ACCOUNT as h,A_CUSTOMER g
                           where h.CUST_NO=g.CUSTOMER_NO AND h.AC_NO IN (:list)";

请注意,在我的示例中,我只有一个开始"(引用)和一个结束"

于 2013-10-11T11:17:29.380 回答
0

Rippo 对空格和引号的看法是正确的,此外我注意到我仍然指的是 HQL 中的类名称而不是别名参考名称

我从“ A_ACCOUNT as h left join A_CUSTOMER g on”变成了“ from A_ACCOUNT as h left join h.CUSTOMER g on

应该是

string qry = @"select h.AC_NO,h.BRANCH_CODE,h.CUST_NO,h.CCY,h.AVL_BAL ,g.ADDRESS_LINE"+
               "from A_ACCOUNT as h left join h.CUSTOMER  g on "+
               "h.CUST_NO=g.CUSTOMER_NO"+ 
               "where  h.AC_NO IN (:list)";
于 2013-10-13T17:01:27.220 回答