Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 518245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:55:02+00:00 2026-05-13T07:55:02+00:00

In my MSSQL server I have a SQL view called AllFavourite. In order to

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T07:55:03+00:00Added an answer on May 13, 2026 at 7:55 am

    The query tag expects a HQL query:

    <query name="GetAllFavouriteByUserId">
        <![CDATA[
        from AllFavourite where UserId = :UserId
        ]]>
    </query>
    

    If you want to write a native sql query you should use the sql-query tag:

    <sql-query name="GetAllFavouriteByUserId">
        <return alias="foo" class="Foo"/>
        <![CDATA[
        SELECT {foo.ID} as {foo.ID}, 
               {foo}.NAME AS {foo.Name} 
        FROM sometable 
        WHERE {foo}.ID = :UserId
        ]]>
    </sql-query>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

MS SQL Server 2000 I have a column in Table A called Name. I
I have a view which if i try to edit, MS SQL Server Management
Does anyone have experience with using indexed view in MS SQL Server 2008? I
MS SQL Server doesn't have row level triggers, correct? If I needed to insert
I have an MS SQL server application where I have defined my relationships and
In MS SQL Server Management Studio 2005: If you have the setting Tools|Options|Scripting|Include IF
On MS SQL Server 2005, I have a table that I want to replace.
I have a table in an MS SQL Server db. I want to create
We have a number of MS SQL Server 2005 installations (both Standard and Enterprise
I am using MS SQL Server 2005, I have dates stored in epoch time

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.