NHibernate连接查询抛出错误

时间:2014-07-25 05:02:40

标签: c# nhibernate join

我正在尝试实现连接查询。我需要获取列id的名称

string hql = "select new TerminalStateReportItem(p.Ip, p.Error, p.Terminal_Id, p.Last_Activity, p.Current_Page, p.Ping, u.Name) from TerminalState p join User u on p.Terminal_Id=u.Id";
var query = new SimpleQuery<TerminalStateReportItem>(hql);
return query.Execute();

我收到错误

  

Castle.ActiveRecord.Framework.ActiveRecordException:无法为TerminalStateReportItem执行ExecuteQuery ---&gt; NHibernate.Hql.Ast.ANTLR.SemanticException:加入的路径! [从KioskServer.TerminalState.TerminalState p中选择新的TerminalStateReportItem(p.Ip,p.Error,p.Terminal_Id,p.Last_Activity,p.Current_Page,p.Ping,u.Name)在p.Terminal_Id = u上加入User1 u。 ID]

我的课程包装

namespace KioskServer.Reports
{
    [ActiveRecord]
    public class TerminalStateReportItem
    {
        public TerminalStateReportItem()
        {
        }

        public TerminalStateReportItem(string ip, string error, int terminalId, DateTime lastActivity, string currentPage, int ping, string name)
        {
            Ip = ip;
            Error = error;
            Terminal_Id = terminalId;
            Last_Activity = lastActivity;
            Current_Page = currentPage;
            Ping = ping;
            Name = name;
        }

        [PrimaryKey]
        public string Ip { get; set; }
        public string Error { get; set; }
        public int Terminal_Id { get; set; }
        public DateTime Last_Activity { get; set; }
        public string Current_Page { get; set; }
        public int Ping { get; set; }
        public string Name{ get; set; }
    }
}

我的班级表

namespace KioskServer.TerminalState
{
    [ActiveRecord("terminal_state")]
    public class TerminalState
    {
        public TerminalState(){
        }

        public TerminalState(string ip, string error, int terminalId, DateTime lastActivity, string currentPage, int ping)
        {
            Ip = ip;
            Error = error;
            Terminal_Id = terminalId;
            Last_Activity = lastActivity;
            Current_Page = currentPage;
            Ping = ping;
        }

        [PrimaryKey]
        public string Ip { get; set; }
        [Property]
        public string Error { get; set; }
        [Property]
        public int Terminal_Id { get; set; }
        [Property]
        public DateTime Last_Activity { get; set; }
        [Property]
        public string Current_Page { get; set; }
        [Property]
        public int Ping { get; set; }
    }
}

没有正常加入所有作品。也许是因为我的班级在不同的文件夹中?

namespace KioskServer.Data.Users
{
    [ActiveRecord("users",
        DiscriminatorColumn = "usertype",
        DiscriminatorType = "string",
        DiscriminatorValue = "user")]
    public class User : Entity
    {
        [Property]
        public string Name { get; set; }

        [Property]
        public string Login { get; set; }

        [Property]
        public string Password { get; set; }

        [Property]
        public DateTime Date { get; set; }

        [BelongsTo]
        public Dealer Dealer { get; set; }
    }
}

我做了一个查询

string hql = "select new TerminalStateItem(p.Ip, p.Error, p.Terminal_Id, p.Last_Activity, p.Current_Page, p.Ping, u.Name) from TerminalState p join p.User u";

错误

NHibernate.QueryException: could not resolve property: User of: KioskServer.TerminalState.TerminalState [select new TerminalStateItem(p.Ip, p.Error, p.Terminal_Id, p.Last_Activity, p.Current_Page, p.Ping, u.Name) from KioskServer.TerminalState.TerminalState p join p.User u]

2 个答案:

答案 0 :(得分:0)

HQL是一种适用于您的域模型的语言。它不是SQL。这意味着什么?

在SQL中你可以说:

SELECT **
FROM  TerminalState p   -- (table TerminalState)
JOIN User1 u            -- (table User1)
ON p.Terminal_Id = u.Id -- (the relation   

在HQL中你不能!因为ON由路径的关系定义。所以 Exception 在这里说:

  

加入的预期路径!

域关系,对象之间的路径,例如这将在HQL中起作用:

select new p.Ip, u.Name ...
from TerminalState p 
// now the PATH
join p.User u  // not needed on p.Terminal_Id=u.Id";

ON (NHibernate生成的SQL语法,没有我们明确的定义)必须来自映射。在TerminalState上必须有User属性映射为many-to-one ...

在此处查看更多信息:

如果你需要一些特殊的SQL,你仍然可以使用NHibernate来获取它,只需使用:

session.CreateSQLQuery() // could contain any valid SQL

而不是session.CreateQuery() (期待HQL)

答案 1 :(得分:0)

你应该有一个映射
[属性] 公共用户用户{get;组; }

在TerminalStateReportItem类中的

然后你可以查询。