0

次のエラーが表示されます:「メッセージ: 1 つ以上の必須パラメーターに値が指定されていません。」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;
        }
    }
}

名前付きクエリが必要とする値を提供するにはどうすればよいですか?

4

1 に答える 1

2

このエラーがすべてを物語っています。名前付きクエリを取得し、それを実行したいということです。ただし、コードでわかるように、名前付きクエリには 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>();
于 2010-02-22T13:59:14.177 に答える