在带有导入类的 hbm 中使用命名查询
在我的 MSSQL 服务器中,我有一个名为 AllFavourite 的 SQL 视图。为了将数据加载到我的 DTO 类中,我的 hbm.xml 文件中有以下内容...
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="Domain.Model.Entities" assembly="Domain.Model">
<import class="AllFavourite"/>
</hibernate-mapping>
在我的代码中,我有以下内容。
public IList<AllFavourite> GetFavourites(int userId)
{
var query = Session
.CreateSQLQuery("SELECT * FROM AllFavourite where UserId=:UserId")
.SetInt32("UserId", userId)
.SetResultTransformer(new AliasToBeanResultTransformer(typeof(AllFavourite)));
return query.List<AllFavourite>();
}
这非常有效,并产生了我想要的结果,但是我想将 SQL 从代码移动到 hbm.xml 文件中的命名查询中。所以我的 hbm.xml 文件现在看起来像这样
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="Domain.Model.Entities" assembly="Domain.Model">
<import class="AllFavourite"/>
<query name="GetAllFavouriteByUserId">
<![CDATA[
SELECT * FROM AllFavourite WHERE UserId=:UserId
]]>
</query>
</hibernate-mapping>
,我的代码现在看起来像这样
public IList<AllFavourite> GetFavourites(int userId)
{
var query = Session
.GetNamedQuery("GetAllFavouriteByUserId")
.SetInt32("UserId", userId)
.SetResultTransformer(new AliasToBeanResultTransformer(typeof(AllFavourite)));
return query.List<AllFavourite>();
}
但是当我运行这个时,我收到一个错误:-
参数 UserId 不存在 [SELECT * FROM 中的命名参数 所有收藏 WHERE UserId=:UserId]
所以我的问题是否可以以这种方式使用命名查询?
In my MSSQL server I have a SQL view called AllFavourite. In order to load the data into my DTO class I have the following in my hbm.xml file...
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="Domain.Model.Entities" assembly="Domain.Model">
<import class="AllFavourite"/>
</hibernate-mapping>
In my code I have the following.
public IList<AllFavourite> GetFavourites(int userId)
{
var query = Session
.CreateSQLQuery("SELECT * FROM AllFavourite where UserId=:UserId")
.SetInt32("UserId", userId)
.SetResultTransformer(new AliasToBeanResultTransformer(typeof(AllFavourite)));
return query.List<AllFavourite>();
}
This works great and produces the results that I am after, however I would like to move the SQL from code into a named query into the hbm.xml file. So my hbm.xml file now looks like this
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="Domain.Model.Entities" assembly="Domain.Model">
<import class="AllFavourite"/>
<query name="GetAllFavouriteByUserId">
<![CDATA[
SELECT * FROM AllFavourite WHERE UserId=:UserId
]]>
</query>
</hibernate-mapping>
and my code now looks like this
public IList<AllFavourite> GetFavourites(int userId)
{
var query = Session
.GetNamedQuery("GetAllFavouriteByUserId")
.SetInt32("UserId", userId)
.SetResultTransformer(new AliasToBeanResultTransformer(typeof(AllFavourite)));
return query.List<AllFavourite>();
}
However when I run this I get an error:-
Parameter UserId does not exist as a
named parameter in [SELECT * FROM
AllFavourite WHERE UserId=:UserId]
So my question is it possible to use a named query in this manner?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
query
标签需要 HQL 查询:如果您想编写本机 sql 查询,则应使用
sql-query
标签:The
query
tag expects a HQL query:If you want to write a native sql query you should use the
sql-query
tag:你不需要这个吗?
Don't you need this?