NHibernate HQL 内部连接(SQL 服务器,Visual C#)
NHibernate HQL Inner Join (SQL Server,Visual C#)
我想在内部连接中使用 HQL。但是,抛出查询语法异常。
这是我的 C# 代码:
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);
}
这里的重点是
- CROSS JOIN 如果没有映射关系,
- 加入现有(映射)关系。
所以,万一没有映射关系 Question
到 Answer
- 我们仍然可以这样查询它:
// 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";
如果我们有映射 Answer.Question
和 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";
检查
- 14.2. The from clause (关于 CROSS JOIN 的详细信息)
- 14.3. Associations and joins (与引用相关的标准 JOIN)
我想在内部连接中使用 HQL。但是,抛出查询语法异常。
这是我的 C# 代码:
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);
}
这里的重点是
- CROSS JOIN 如果没有映射关系,
- 加入现有(映射)关系。
所以,万一没有映射关系 Question
到 Answer
- 我们仍然可以这样查询它:
// 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";
如果我们有映射 Answer.Question
和 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";
检查
- 14.2. The from clause (关于 CROSS JOIN 的详细信息)
- 14.3. Associations and joins (与引用相关的标准 JOIN)