NHibernate的。 我有3张桌子:
员工{ PK:EmployeeId ,姓名,姓氏......}
项目{ PK:ProjectId ,日期,名称,...}
EmployeebyProject { PK:FK:EmployeeId,ProjectId ,date,...}
我需要制作一些CRUD,此时我在tabla EmployeebyProject中有一些记录。所以我试着找到这个记录的人。这是使用条件的方法,但发生此错误:“GenericADOException,无法执行查询”“列EmployeeId无效”,“列ProductId无效”。 此条件的问题不是转到Employee和Project表来查询idEmployee和idProject。 那我该怎么做呢?
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;
}
}
这是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>
答案 0 :(得分:0)
您从.Add(Restrictions.Eq("EmployeeId", idEmployee))
调用的EmployeeId属性是错误的,EmployeeId不属于EmployeeProject实体。它属于Employee实体。因此,您的查询应该重写为
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;
}
}
我认为您应该拨打.CreateCriteria("Employee", "EmployeeAl")
而不是.CreateAlias("Employee", "EmployeeAl")
而不是{{1}}