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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:50:13+00:00 2026-05-18T01:50:13+00:00

I’ve been using NHibernate for a while now but mostly with convention based auto

  • 0

I’ve been using NHibernate for a while now but mostly with convention based auto persistence model generation and then performing basic CRUD operations on the objects.

I now have that need to execute a rather nasty query against a legacy system along with a hand full of parameters (most of the where clause needs to be parameterized). I’m not clear on whether I should be defining the individual objects and then trying to implement the complex query OR simply using native SQL to define the query. I’m also not clear on the correct syntax to define the query in an hbm.xml file. The native SQL query follows. Thanks to anyone who can help me sort this out.

WITH CTE AS ( SELECT
    substr(KEY1||KEY2||KEY3||'-'||digits(KEY4)||'-'||digits(KEY5),1,17) KEY_ID,
    SUM(CASE WHEN BATCH between 200801001 and 200812999 THEN TRANSACTION_AMOUNT ELSE 0 END) AS _2008AMOUNT,
    SUM(CASE WHEN BATCH between 200901001 and 200912999 THEN TRANSACTION_AMOUNT ELSE 0 END) AS _2009AMOUNT,
    SUM(CASE WHEN BATCH between 201001001 and 201012999 THEN TRANSACTION_AMOUNT ELSE 0 END) AS _2010AMOUNT

    FROM _SCHEMA.TRANSACTIONS

    WHERE OWNER='02'
    AND TRANSACTION_TYPE in ('A','B')
    AND BATCH between 200801000 AND 201012999

    GROUP BY KEY1, KEY2, KEY3, KEY4, KEY5

    HAVING SUM(CASE WHEN BATCH between 200801001 and 200812999 THEN TRANSACTION_AMOUNT ELSE 0 END)  0
    OR SUM(CASE WHEN BATCH between 200901001 and 200912999 THEN TRANSACTION_AMOUNT ELSE 0 END)  0
    OR SUM(CASE WHEN BATCH between 201001001 and 201012999 THEN TRANSACTION_AMOUNT ELSE 0 END)  0
    )


    SELECT
    B.OWNER
    ,CTE1.MAX_ITEM_AMOUNT
    ,CTE1.SUM_ITEM_AMOUNT
    ,B.PRIMARY_SECONDARY_IDENTITY
    ,B.PRIMARY_ID
    ,B.PRIMARY_NAME
    ,B.KEY_ID
    ,B.KEY_AMOUNT
    ,CASE(B.KEYCLASS) WHEN 'C' THEN 'CLASS1' WHEN 'S' THEN 'CLASS2' WHEN 'M' THEN 'CLASS3' WHEN 'F' THEN 'CLASS4' END AS KEYCLASS
    ,NA.PRIMARY_CT AS PRIMARY_CITY
    ,NA.PRIMARY_ST AS PRIMARY_STATE
    ,B.KEY_EFFECTIVE_DATE
    ,B.KEY_ENTRY_DATE
    ,CTE._2008AMOUNT
    ,CTE._2009AMOUNT
    ,CTE._2010AMOUNT
    ,B.EMPLOYEE_NAME

    FROM CTE

    JOIN _SCHEMA.TABLE2 B ON CTE.KEY_ID = B.KEY_ID

    JOIN (
    SELECT
    PRIMARY_SECONDARY_IDENTITY
    , MAX(ITEM_AMOUNT) AS MAX_ITEM_AMOUNT
    , SUM(ITEM_AMOUNT) AS SUM_TTEM_AMOUNT
    FROM CTE
    JOIN _SCHEMA.TABLE2 B ON CTE.KEY_ID = B.KEY_ID
    GROUP BY PRIMARY_SECONDARY_IDENTITY
    ) CTE1 ON CTE1.PRIMARY_SECONDARY_IDENTITY = B.PRIMARY_SECONDARY_IDENTITY

    JOIN _SCHEMA.TABLE3 NA ON B.PRIMARY_ID = NA.KEY1 || DIGITS(NA.KEY2)

    ORDER BY MAX_ITEM_AMOUNT DESC, PRIMARY_SECONDARY_IDENTITY
    
  • 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-18T01:50:13+00:00Added an answer on May 18, 2026 at 1:50 am

    This kind of transaction whatsoever shall be processed through named queries.

    NHibernate allows for some pre-configured named queries which a simple CRUD operation can’t do. I suggest you take an eye out the use of these in the NHibernate documentation.

    15.2. Named SQL queries

    On the other hand, you may be interested in native SQL queries.

    9.3.5. Queries in native SQL

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

Sidebar

Related Questions

No related questions found

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.