0

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 query 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>
1
  • can you try var employeebyProject = session .CreateCriteria(typeof(EmployeebyProject)) .Add(Restrictions.Eq("Employee", session.Load<Employee>(idEmployee))) .Add(Restrictions.Eq("Project", session.Load<Employee>(idProject))) .UniqueResult<EmployeebyProject>();
    – Firo
    Jul 21, 2011 at 7:20

1 Answer 1

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.