本文介绍了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;
检查
- (有关CROSS JOIN的详细信息)
- (与引用相关的标准连接)
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
- 14.2. The from clause (details about CROSS JOIN)
- 14.3. Associations and joins (standard JOIN related to references)
这篇关于NHibernate HQL内部连接(SQL Server,Visual C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!