Question

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?

Was it helpful?

Solution

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>();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top