具有复合 ID 错误的标准

发布于 2024-11-25 10:25:23 字数 1808 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灼痛 2024-12-02 10:25:23

您从 .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;
        }
    }

,我认为您应该调用 .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 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文