我一直在尝试使用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:
答案 0 :(得分: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)";
请注意,在我的示例中,我只有一个已启动的"
(引用)和一个结尾"
。
答案 1 :(得分: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)";