2010-02-22 6 views
0

"메시지 : 하나 이상의 필수 매개 변수에 값이 지정되지 않았습니다."라는 오류가 발생합니다. MBUnit에서 코드를 테스트하려고 할 때.NHibernate 명명 된 쿼리에서 매개 변수의 값을 제공하는 방법

<?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; 
     } 
    } 
} 

어떻게 쿼리의 요구라는 것을 가치를 제공합니까?

답변

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>(); 
+0

확인 완벽 .... 그래서 어떻게 매개 변수에 대한 값을 제공합니까? – MadSeb

+0

도전은 몇 가지 이유로 내 코드에서 "세션"에 액세스 할 수 없다는 것입니다 ... 그래서 매핑에서 매개 변수를 설정해야합니다 ... 어떤 방식 으로든 할 수 있습니까? – MadSeb

+0

SELECT * FROM BestStudents WHERE CourseId = 5 – Will

관련 문제