如何为 NHibernate 命名查询中的参数提供值
我收到以下错误:“消息:没有为一个或多个必需参数给出值。”当我尝试测试 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误说明了一切:
您检索命名查询并想要执行它。
不过,正如您在代码中所看到的,命名查询在其 where 子句中有一个参数。
您必须为命名查询提供参数值,否则无法执行。
像这样的东西:
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: