NHibernate HQL内部连接(SQL Server,Visual C#) [英] NHibernate HQL Inner Join (SQL Server,Visual C#)

查看:147
本文介绍了NHibernate HQL内部连接(SQL Server,Visual C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用inner join来使用HQL。但是,引发了查询语法异常。



这是我的C#代码:

  string sqlQuery =Select fq FROM Answers as fq INNER JOIN Questions as q+ 
on fq.questionId = q.questionId;

IList结果;
int count = 0;

尝试
{
using(ISession session = ConnectionModule.OpenSession())
{
IQuery query = session.CreateQuery(sqlQuery);
session.CreateCriteria(typeof(Answers));
Result = query.List();

$ b $ catch(Exception ex)
{
MessageBox.Show(ex.Message +\\\
+ ex.InnerException);

$ / code $ / pre

解决方案

如果没有映射关系,


  • CROSS JOIN

  • 加入现有(映射)关系。 li>


因此,如果没有映射关系问题到<$ c $

  // //代替INNER JOIN我们可以这样查询:使用'逗号'来产生CROSS JOIN 
//而不是ON我们需要WHERE
// string sqlQuery =Select fq FROM Answers as fq,INNER JOIN Questions as q+
// on fq.questionId = q.questionId;

string sqlQuery =Select fq FROM Answers as fq,Questions as q+
WHERE fq.questionId = q.questionId;

如果我们映射 Answer.Question IList< Answer>问题.Answers

  //参考(C#)是如何表达ON 
string sqlQuery =Select fq FROM Answers as fq INNER JOIN fq.Questions as q;

检查


I want to using HQL with inner Join. But, a query syntax exception is thrown.

This is my C# code:

string sqlQuery = "Select fq FROM Answers as fq INNER JOIN Questions as q " +
    " on fq.questionId=q.questionId";

IList Result;
int count = 0;

try
{
    using (ISession session = ConnectionModule.OpenSession())
    {
        IQuery query = session.CreateQuery(sqlQuery);
        session.CreateCriteria(typeof(Answers));
        Result = query.List();
    }
}
catch(Exception ex) 
{
    MessageBox.Show(ex.Message+"\n"+ex.InnerException);
}

解决方案

The point here is

  • CROSS JOIN if there are no mapped relations,
  • JOIN on existing (mapped) relations.

So, in case, there is no mapped relation Question to Answer - we still can query it like this:

// instead of INNER JOIN we use 'comma' to produce CROSS JOIN
// instead of ON we need WHERE
// string sqlQuery = "Select fq FROM Answers as fq, INNER JOIN Questions as q "+ 
//  "on fq.questionId=q.questionId";

string sqlQuery = "Select fq FROM Answers as fq, Questions as q " +
    " WHERE fq.questionId=q.questionId";

In case we have mapping Answer.Question and IList<Answer> Question.Answers

// the Reference (C#) is the way how to express ON
string sqlQuery = "Select fq FROM Answers as fq INNER JOIN fq.Questions as q";

Check the

这篇关于NHibernate HQL内部连接(SQL Server,Visual C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆