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

  • SEARCH
  • Home
  • 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 408753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:43:21+00:00 2026-05-12T17:43:21+00:00

I have a stored procedure in my database that calculates the distance between two

  • 0

I have a stored procedure in my database that calculates the distance between two lat/long pairs. This stored procedure is called “DistanceBetween”. I have a SQL statement allows a user to search for all items in the Items table ordered by the distance to a supplied lat/long coordinate. The SQL statement is as follows:

SELECT Items.*, dbo.DistanceBetween(@lat1, @lat2, Latitude, Longitude) AS Distance
FROM Items
ORDER BY Distance

How do I go about using this query in NHibernate? The Item class in my domain doesn’t have a “Distance” property since there isn’t a “Distance” column in my Items table. The “Distance” property really only comes into play when the user is performing this search.

  • 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-12T17:43:21+00:00Added an answer on May 12, 2026 at 5:43 pm

    There are basically three approaches that can be used, some of which have already been discussed:

    1. Use an HQL query or CreateCriteria/ICriteria query; downsides: it is not part of the entities/DAL; upsides: it is flexible;
    2. Use a property mapping with a formula; downsides: it is not always feasible or possible, performance can degrade if not careful; upsides: it the calculation an integral part of your entities;
    3. Create a separate XML HBM mapping file and map to a separate (to be created) entity; downsides: it is not part of the base entities; upsides: you only call the SP when needed, full control of mapping / extra properties / extensions, can use partial or abstract classes to combine with existing entities.

    I’ll briefly show an example of option 2 and 3 here, I believe option 1 has been sufficiently covered by others earlier in this thread.

    Option two, as in this example, is particularly useful when the query can be created as a subquery of a select statement and when all needed parameters are available in the mapped table. It also helps if the table is not mutable and/or is cached as read-only, depending on how heavy your stored procedure is.

    <class name="..." table="..." lazy="true" mutable="false>
      <cache usage="read-only" />
    
        <id name="Id" column="id" type="int">
          <generator class="native" />
        </id>
        <property name="Latitude" column="Latitude" type="double" not-null="true" />
        <property name="Longitude" column="Longitude" type="double" not-null="true" />
    
        <property name="PrijsInstelling"
            formula="(dbo.DistanceBetween(@lat1, @lat2, Latitude, Longitude))"
            type="double" />
    
        ... etc
    </class>
    

    If the above is not possible due to restrictions in the mappings, problems with caching or if your current cache settings retrieve one by one instead of by bigger amounts and you cannot change that, you should consider an alternate approach, for instance a separate mapping of the whole query with parameters. This is quite close to the CreateSqlQuery approach above, but forces the result set to be of a certain type (and you can set each property declaratively):

    <sql-query flush-mode="never" name="select_Distances">
        <return
            class="ResultSetEntityClassHere,Your.Namespace"
            alias="items"
            lock-mode="read" >
    
            <return-property name="Id" column="items_Id" />
            <return-property name="Latitude" column="items_Latitude" />
            <return-property name="Longitude" column="items_Longitude" />
            <return-property name="Distance" column="items_Distance" />
        </return>
    
        SELECT 
            Items.*, 
            dbo.DistanceBetween(@lat1, @lat2, Latitude, Longitude) AS Distance
        FROM Items
        WHERE UserId = :userId
    
    </sql-query>
    

    You can call this query as follows:

    List<ResultSetEntityClassHere> distanceList = 
        yourNHibernateSession.GetNamedQuery("select_Distances") 
            .SetInt32("userId", currentUserId)  /* any params go this way */
            .SetCacheable(true)                 /* it's usually good to cache */
            .List<ResultSetEntityClassHere>();  /* must match the class of sql-query HBM */
    

    Depending on your needs, you can choose an approach. I personally use the following rule of thumb to decide what approach to use:

    • Is the calculation light or can it be cached? Use formula approach;
    • Are parameters needed to be sent to the SP/SQL? Use sql-query + mapping approach;
    • Is the structure of the query (very) variable? Use ICriteria or HQL approach through code.

    About the ordering of the data: when you choose the “formula” or the “sql-query mapping” approach you’ll have to do the ordering when you retrieve the data. This is not different then with retrieving data through your current mappings.

    Update: terrible edit-mistake corrected in the sql-query XML.

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

Sidebar

Related Questions

I have a stored procedure that will search the whole database. This is the
I have a stored procedure in a MS-SQL 2005 database that: Creates two temp
So I have a stored procedure called Spr_EventLogCreate defined in my database. I have
I have stored-procedure in Oracle database like this: create or replace PROCEDURE EDYTUJ_PRACOWNIKA (PR_IMIE
I have a stored procedure in a SQL Server database that returns a list
I have stored procedure that insanely times out every single time it's called from
I have a stored procedure in my database that takes a table value parameter,
I have a stored procedure in a SQL Server 2008 database. This stored procedure
I have a stored procedure in an old SQL 2000 database that takes a
I have a stored procedure, in one database within my SQL server, that sets

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.