无法解析属性:empAlias

本文关键字:empAlias 属性 | 更新日期: 2023-09-27 18:06:27

我的目的是在Project中查找Employee信息,其中ProjectNumber> 2

这是我的查询:

List<EmployeeDAO> empList = new List<EmployeeDAO>();
using (ISession mySession = SessionFactory().OpenSession())
{
    using (ITransaction myTransaction = mySession.BeginTransaction())
    {
        EmployeeDAO empDaoAlias = null;
        Employee empAlias = null;
        Group groupAlias = null;
        Project projectAlias = null;
        Rank rankAlias = null;
        EmployeeFunctionInProject efpAlias = null;
        empList = mySession.QueryOver<Employee>(() => empAlias)
            .JoinAlias(() => empAlias.Rank, () => rankAlias)
            .JoinAlias(() => empAlias, () => efpAlias.Employee)
            .Inner.JoinAlias(() => efpAlias.Project, () => projectAlias)
                .Where(() => projectAlias.ProjectNumber > 2)
            .Select(Projections.ProjectionList()
            .Add(Projections.Property(() => empAlias.Visa).WithAlias(() => empDaoAlias.Visa))
            .Add(Projections.Property(() => empAlias.FirstName).WithAlias(() => empDaoAlias.FirstName))
            .Add(Projections.Property(() => empAlias.LastName).WithAlias(() => empDaoAlias.LastName))
            .Add(Projections.Property(() => empAlias.FirstName).WithAlias(() => empDaoAlias.Project))
            .Add(Projections.Property(() => rankAlias.Name).WithAlias(() => empDaoAlias.Rank)))
            .TransformUsing(Transformers.AliasToBean<EmployeeDAO>())
            .List<EmployeeDAO>().ToList();
    }
}
return empList;

我得到了这个错误

无法解析属性:empAlias of: MyWeb10.Models.Employee

我怀疑问题发生在.JoinAlias(() => empAlias, () => efpAlias.Employee)

My Employee类

public class Employee {
    private int _id;
    private Rank _rank;
    private string _visa;
    private string _firstName;
    private string _lastName;
    private DateTime? _birthday;
    private string _university;
    private DateTime _rowversion;
    public Employee() {
        EmployeeFunctionInProject = new List<EmployeeFunctionInProject>();
        Group = new List<Group>();
    }
    public virtual int Id {
        get {
            return this._id;
        }
        set {
            this._id = value;
        }
    }
    public virtual Rank Rank {
        get {
            return this._rank;
        }
        set {
            this._rank = value;
        }
    }
    public virtual string Visa {
        get {
            return this._visa;
        }
        set {
            this._visa = value;
        }
    }
    public virtual string FirstName {
        get {
            return this._firstName;
        }
        set {
            this._firstName = value;
        }
    }
    public virtual string LastName {
        get {
            return this._lastName;
        }
        set {
            this._lastName = value;
        }
    }
    public virtual DateTime? Birthday {
        get {
            return this._birthday;
        }
        set {
            this._birthday = value;
        }
    }
    public virtual string University {
        get {
            return this._university;
        }
        set {
            this._university = value;
        }
    }
    public virtual DateTime Rowversion {
        get {
            return this._rowversion;
        }
        set {
            this._rowversion = value;
        }
    }
    public virtual IList<EmployeeFunctionInProject> EmployeeFunctionInProject { get; set; }
    public virtual IList<Group> Group { get; set; }
}

我的EmployeeFunctionInProject类

public class EmployeeFunctionInProject {
    private int _id;
    private Function _function;
    private Project _project;
    private Employee _employee;
    private DateTime _rowversion;
    public virtual int Id {
        get {
            return this._id;
        }
        set {
            this._id = value;
        }
    }
    public virtual Function Function {
        get {
            return this._function;
        }
        set {
            this._function = value;
        }
    }
    public virtual Project Project {
        get {
            return this._project;
        }
        set {
            this._project = value;
        }
    }
    public virtual Employee Employee {
        get {
            return this._employee;
        }
        set {
            this._employee = value;
        }
    }
    public virtual DateTime Rowversion {
        get {
            return this._rowversion;
        }
        set {
            this._rowversion = value;
        }
    }
}

My Employee映射

<hibernate-mapping assembly="MyWeb10" namespace="MyWeb10.Models" xmlns="urn:nhibernate-mapping-2.2">
  <class name="Employee" table="EMPLOYEE" lazy="true" >
    <id name="Id" column="ID">
      <generator class="identity" />
    </id>
    <many-to-one name="Rank">
      <column name="RANK" sql-type="int" not-null="true" />
    </many-to-one>
    <property name="Visa">
      <column name="VISA" sql-type="varchar" not-null="true" unique="true" />
    </property>
    <property name="FirstName">
      <column name="FIRST_NAME" sql-type="varchar" not-null="true" />
    </property>
    <property name="LastName">
      <column name="LAST_NAME" sql-type="varchar" not-null="true" />
    </property>
    <property name="Birthday">
      <column name="BIRTHDAY" sql-type="date" not-null="false" />
    </property>
    <property name="University">
      <column name="UNIVERSITY" sql-type="varchar" not-null="true" />
    </property>
    <property name="Rowversion">
      <column name="ROWVERSION" sql-type="timestamp" not-null="true" />
    </property>
    <bag name="EmployeeFunctionInProject" inverse="true">
      <key column="EMPLOYEE" />
      <one-to-many class="EmployeeFunctionInProject" />
    </bag>
    <bag name="Group" inverse="true">
      <key column="LEADER" />
      <one-to-many class="Group" />
    </bag>
  </class>
</hibernate-mapping>

My EmployeeFunctionInProject映射

<hibernate-mapping assembly="MyWeb10" namespace="MyWeb10.Models" xmlns="urn:nhibernate-mapping-2.2">
  <class name="EmployeeFunctionInProject" table="EMPLOYEE_FUNCTION_IN_PROJECT" lazy="true" >
    <id name="Id" column="ID">
      <generator class="identity" />
    </id>
    <many-to-one name="Function">
      <column name="FUNCTION" sql-type="int" not-null="false" />
    </many-to-one>
    <many-to-one name="Project">
      <column name="PROJECT" sql-type="int" not-null="false" />
    </many-to-one>
    <many-to-one name="Employee">
      <column name="EMPLOYEE" sql-type="int" not-null="false" />
    </many-to-one>
    <property name="Rowversion">
      <column name="ROWVERSION" sql-type="timestamp" not-null="true" />
    </property>
  </class>
</hibernate-mapping>

无法解析属性:empAlias

在这种情况下,当我们想要通过一些集合/子成员来过滤根/父实体时,我们应该使用子查询(内部SELECT)

EmployeeFunctionInProject efpAlias = null;
Employee empAlias = null;
Project projectAlias = null;
// subquery, selecting two pair table and the Project table
var subquery = QueryOver.Of<EmployeeFunctionInProject>(() => efpAlias)
    .JoinAlias(() => efpAlias.Entity, () => projectAlias)
    // just the ProjectNumber over 2
    .Where(() => projectAlias.ProjectNumber > 2)
    // the ID of an employee
    .Select(x => efpAlias.Employee.Id);
// the root query, over Employee
var list = session.QueryOver<Employee>(() => empAlias)
    .WithSubquery
       .WhereProperty(() => empAlias.Id)
       .In(subquery)
    // the rest of the query
    ...// Take(), Skipe(), Select(), List()

查看更多信息:

  • 16.8。子查询- QueryOver