具有复合 ID 错误的标准
NHibernate。 我有 3 个表:
Employee {PK:EmployeeId, Name, LastName, ...}
Project {PK: ProjectId, date, name, ...}
EmployeebyProject {< strong>PK:FK: EmployeeId, ProjectId, date, ...}
我需要做一些增删改查,此时我在 tabla EmployeebyProject 中有一些记录。所以我试图找人记录这些记录。这是使用条件的方法,但发生了以下错误:“GenericADOException,无法执行查询”“Column EmployeeId is not valid”,“Column ProductId is not valid”。 此条件的问题是无法转到 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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您从
.Add(Restrictions.Eq("EmployeeId", idEmployee))
调用的 EmployeeId 属性是错误的,EmployeeId 不属于 EmployeeProject 实体。它属于 Employee 实体。因此,您的查询应该重写为,我认为您应该调用
.CreateAlias("Employee", "EmployeeAl")
而不是.CreateCriteria("Employee", "EmployeeAl")
代码> 和项目相同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 asand i think that instead of
.CreateCriteria("Employee", "EmployeeAl")
you should call.CreateAlias("Employee", "EmployeeAl")
and the same for the project