hibernate HQL内部连接(SQL Server,Visual c#)

本文关键字:Visual Server SQL HQL 内部 连接 hibernate | 更新日期: 2023-09-27 17:51:09

我想使用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);
}

hibernate HQL内部连接(SQL Server,Visual c#)

关键是

  • 如果没有映射关系,则使用CROSS JOIN,
  • 连接现有的(映射的)关系。

因此,如果没有QuestionAnswer的映射关系,我们仍然可以这样查询:

// 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.QuestionIList<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。from子句(关于CROSS JOIN的详细信息)
  • 14.3。关联和连接(与引用相关的标准连接)