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 253965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:48:25+00:00 2026-05-11T21:48:25+00:00

IN converting over a legacy application we need to convert named query to nhibernate.

  • 0

IN converting over a legacy application we need to convert named query to nhibernate. The problem is that the where clause is being set.

here is the mapping

<resultset name="PersonSet">
<return alias="person" class="Person">
  <return-property column="id" name="Id" />
  <return-property column="ssn" name="Ssn" />
  <return-property column="last_name" name="LastName" />
  <return-property column="first_name" name="FirstName"/>
  <return-property column="middle_name" name="MiddleName" />
</return>
</returnset>

<sql-query name="PersonQuery" resultset-ref="PersonSet" read-only="true" >
  <![CDATA[
  SELECT
  person.ID as {person.Id},
  person.SSN as {person.Ssn},
  person.LAST_NAME as {person.LastName},
  person.MIDDLE_NAME as {person.MiddleName},
  person.FIRST_NAME as {person.FirstName},
  FROM PERSONS as person
  where :value
  ]]>
</sql-query>

and the c# code:

String query = "person.LAST_NAME = 'Johnson'";
HibernateTemplate.FindByNamedQueryAndNamedParam("PersonQuery", "value", query);

The error:

where ?]; ErrorCode []; An expression of non-boolean type specified in a context where a condition is expected, near ‘@p0’.

  • 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-11T21:48:26+00:00Added an answer on May 11, 2026 at 9:48 pm

    This doesn’t work because you try to replace :value with "person.LAST_NAME = 'Johnson'" wanting that the query becomes

    SELECT person.ID, person.SSN, person.LAST_NAME, person.MIDDLE_NAME, person.FIRST_NAME
    FROM PERSONS as person
    where person.LAST_NAME = 'Johnson'
    

    This won’t work. You can only replace the ‘Johnson’ part dynamically not the whole condition. Thus what really gets generated is

    SELECT person.ID, person.SSN, person.LAST_NAME, person.MIDDLE_NAME, person.FIRST_NAME
    FROM PERSONS as person
    where 'person.LAST_NAME = \'Johnson\''
    

    Which obviously isn’t a valid condition for the WHERE-part as there is only a literal but no column and operator to compare the field to.

    If you only have to match against person.LAST_NAME rewrite the xml-sql-query to

    <sql-query name="PersonQuery" resultset-ref="PersonSet" read-only="true" >
      <![CDATA[
      SELECT
      ...
      FROM PERSONS as person
      where person.LAST_NAME = :value
      ]]>
    </sql-query>
    

    And in the C# code set

    String query = "Johnson";
    

    If you need to dynamically filter by different columns or even multiple columns at a time use filters. e.g. like this (i made a few assumptions on you hibernate-mapping file)

    <hibernate-mapping>
      ...
      <class name="Person">
        <id name="id" type="int">
          <generator class="increment"/>
        </id>
        ...
        <filter name="ssnFilter" condition="ssn = :ssnValue"/>
        <filter name="lastNameFilter" condition="lastName = :lastNameValue"/>
        <filter name="firstNameFilter" condition="firstName = :firstNameValue"/>
        <filter name="middleNameFilter" condition="middleName = :middleNameValue"/>
      </class>
      ...
      <sql-query name="PersonQuery" resultset-ref="PersonSet" read-only="true" >
      ...
        FROM PERSONS as person
      ]]>
      </sql-query>
      <!-- note the missing WHERE clause in the PersonQuery -->
      ...
      <filter-def name="ssnFilter">
        <filter-param name="ssnValue" type="int"/>
      </filter-def>
      <filter-def name="lastNameFilter">
        <filter-param name="lastNameValue" type="string"/>
      </filter-def>
      <filter-def name="middleNameFilter">
        <filter-param name="midlleNameValue" type="string"/>
      </filter-def>
      <filter-def name="firstNameFilter">
        <filter-param name="firstNameValue" type="string"/>
      </filter-def>
    </hibernate-mapping>
    

    Now in your code you should be able to do

    String lastName = "Johnson";
    String firstName = "Joe";
    
    //give me all persons first
    HibernateTemplate.FindByNamedQuery("PersonQuery");
    
    //just give me persons WHERE FIRST_NAME = "Joe" AND LAST_NAME = "Johnson"
    Filter filter = HibernateTemplate.enableFilter("firstNameFilter");
    filter.setParameter("firstNameValue", firstName);
    filter = HibernateTemplate.enableFilter("lastNameFilter");
    filter.setParameter("lastNameValue", lastName);
    HibernateTemplate.FindByNamedQuery("PersonQuery");
    
    //oh wait. Now I just want all Johnsons
    HibernateTemplate.disableFilter("firstNameFilter");
    HibernateTemplate.FindByNamedQuery("PersonQuery");
    
    //now again give me all persons
    HibernateTemplate.disableFilter("lastNameFilter");
    HibernateTemplate.FindByNamedQuery("PersonQuery");
    

    If you need still more dynamic queries (e.g. even changing the operator (=, !=, like, >, <, …) or you have to combine restrictions logically (where lastname = “foo” OR firstname” = “foobar”) then it’s definitly time to look into the

    Hibernate Criteria API

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 236k
  • Answers 236k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use Grid.Children.Add to add it to the grid, and Grid.SetRow… May 13, 2026 at 6:25 am
  • Editorial Team
    Editorial Team added an answer That is a very strange result. I am curious why… May 13, 2026 at 6:25 am
  • Editorial Team
    Editorial Team added an answer While I can't answer your questions regarding XML, I believe… May 13, 2026 at 6:25 am

Related Questions

I am working on a Silverlight application that uses WCF. I need to have
I've inherited a project where the class diagrams closely resemble a spider web on
i am converting over a website and using the same .css files. its really
So I'm working on this VB to C# web application migration and came across

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.