开发者

Criteria with composite-id error

开发者 https://www.devze.com 2023-03-21 23:32 出处:网络
NHibernate. I have 3 tables: Employee {PK:EmployeeId, Name, LastName, ...} Project {PK: ProjectId, date, name, ...}

NHibernate. I have 3 tables:

Employee {PK:EmployeeId, Name, LastName, ...}

Project {PK: ProjectId, date, name, ...}

EmployeebyProject {PK:FK: EmployeeId, ProjectId, date, ...}

I need make some CRUD, at this moment I have some records in the tabla EmployeebyProject. So I'm try to Get someone of this records. This is the method using criteria, but happend this error: "GenericADOException, could not execute the query" "Column EmployeeId is not valid", "Column ProductId is not valid". The problem with this criteria is not go to Employee and Project tables to make the que开发者_开发技巧ry for the idEmployee, and idProject. So how can I make this??.

 public EmployeebyProject GetEmployeebyProjectByIdEmployee(int idEmployee, int idProject)
        {
            using (ISession session = NHibernateSessionBuilder.OpenSession())
            {
                var employeebyProject = session
                        .CreateCriteria(typeof(EmployeebyProject))
                        .CreateCriteria("Employee", "Employee")
                        .Add(Restrictions.Eq("EmployeeId", idEmployee))
                        .UniqueResult<EmployeebyProject>();
                return employeebyProject;
            }
        }

this is the mapping for EmployeebyProject:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="AdminProject"
                   namespace="AdminProject.Business.Entity">

  <class name="EmployeebyProject">

    <composite-id>
      <key-many-to-one name="Employee" column="EmployeeId"  class="Employee"></key-many-to-one>
      <key-many-to-one name="Project" column="ProjectId"  class="Project" ></key-many-to-one>
    </composite-id> 

    <property name="DateBegin" type="DateTime"/>
    <property name="DateEnd" type="DateTime"/>

  </class>

</hibernate-mapping>


the EmployeeId property you are calling from .Add(Restrictions.Eq("EmployeeId", idEmployee)) is wrong, the EmployeeId does not belong to the EmployeeProject entity. It belongs to the Employee entity. So your query should be rewritten as

    public EmployeebyProject GetEmployeebyProjectByIdEmployee(int idEmployee, int idProject)
    {
        using (ISession session = NHibernateSessionBuilder.OpenSession())
        {
            var employeebyProject = session
                    .CreateCriteria(typeof(EmployeebyProject))
                    .CreateCriteria("Employee", "EmployeeAl")
                    .CreateCriteria("Project", "ProjectAl")
                    .Add(Restrictions.Eq("EmployeeAl.id", idEmployee))
                    .Add(Restrictions.Eq("ProjectAl.id", idProject))
                    .UniqueResult<EmployeebyProject>();
            return employeebyProject;
        }
    }

and i think that instead of .CreateCriteria("Employee", "EmployeeAl") you should call .CreateAlias("Employee", "EmployeeAl") and the same for the project

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号