如何为 NHibernate 命名查询中的参数提供值

发布于 2024-08-22 02:28:38 字数 1392 浏览 11 评论 0原文

我收到以下错误:“消息:没有为一个或多个必需参数给出值。”当我尝试测试 MBUnit 的代码时。

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="myApplication.Core" namespace="studentTrak">


  <class name="UniversityCourse" table="UniversityCourse" lazy="true">

    <id name="Id" column="ID" type="int">
      <generator class="native" />
    </id>

    <property name="Name" column="Name" type="string"  not-null="true"/>
    <property name="Description" column="Description" type="string" />


    <many-to-one name="BestStudent" class="Student"/>
    <loader query-ref="GetBestStudent"/>

  </class>
  <sql-query name="GetBestStudent" callable="true">
    <return class="Student">
    </return>
    SELECT *  FROM BestStudents WHERE CourseId = ?
  </sql-query>
</hibernate-mapping>

实体的代码是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace studentTrak
{
    public class UniversityCourse 
    {

        public virtual int Id { get; set; }
        public virtual String Description { get; set; }
        public virtual String Name {get;set;}


        public virtual Student BestStudent
        {
            get;
            set;
        }
    }
}

如何提供命名查询所需的值?

I get the following error : "Message: No value given for one or more required parameters." when I try to test the code from MBUnit.

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="myApplication.Core" namespace="studentTrak">


  <class name="UniversityCourse" table="UniversityCourse" lazy="true">

    <id name="Id" column="ID" type="int">
      <generator class="native" />
    </id>

    <property name="Name" column="Name" type="string"  not-null="true"/>
    <property name="Description" column="Description" type="string" />


    <many-to-one name="BestStudent" class="Student"/>
    <loader query-ref="GetBestStudent"/>

  </class>
  <sql-query name="GetBestStudent" callable="true">
    <return class="Student">
    </return>
    SELECT *  FROM BestStudents WHERE CourseId = ?
  </sql-query>
</hibernate-mapping>

The code for the entity is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace studentTrak
{
    public class UniversityCourse 
    {

        public virtual int Id { get; set; }
        public virtual String Description { get; set; }
        public virtual String Name {get;set;}


        public virtual Student BestStudent
        {
            get;
            set;
        }
    }
}

How do I provide the value that the named query needs?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

尽揽少女心 2024-08-29 02:28:38

错误说明了一切:
您检索命名查询并想要执行它。
不过,正如您在代码中所看到的,命名查询在其 where 子句中有一个参数。
您必须为命名查询提供参数值,否则无法执行。

像这样的东西:

IQuery q = session.GetNamedQuery ("GetBestStudent");
q.SetInt32 (0, someCourseId); // since the parameter has no name, you'll have to use the position of the parameter.
var result = q.UniqueResult<Student>();

The error says it all:
you retrieve the named query, and want to execute it.
Although, as you can see in your code, the named query has a parameter in its where clause.
You have to provide the named query with a value for its parameter, otherwise it cannot be executed.

Something like this:

IQuery q = session.GetNamedQuery ("GetBestStudent");
q.SetInt32 (0, someCourseId); // since the parameter has no name, you'll have to use the position of the parameter.
var result = q.UniqueResult<Student>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文