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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:38:07+00:00 2026-05-26T19:38:07+00:00

All, Does anyone know how I can direct the SqlQuery class to use aliases

  • 0

All,

Does anyone know how I can direct the SqlQuery class to use aliases for columns returned on a Non Managed Entity query?

I’m attempting to use the SqlQuery Hibernate class to create a List of non-entity POJO’s, but am running into trouble trying to alias the columns in the SQL query. When I attempt to put aliases in my sql (e.g SELECT o.id as orderId, ps.code as prescriptionStatus...), hibernate complains that it can’t find column “x”, where column “x” is the non-aliased column name (e.g “id” instead of “orderId”).

If I don’t alias my returned columns, all is well, but my POJO is then forced to have properties with the non-aliased field names, or I have to manage mappings within the POJO (getters with nice names returning the non-aliased field names).

Here’s my code

//TODO:  change builder to a name query --jg

    StringBuilder sql = new StringBuilder();

    sql.append("SELECT o.id,\n");
    sql.append("       pet.name,\n");
    sql.append("       o.order_date,\n");
    sql.append("       rx_view.prescription_id,\n");
    sql.append("       rx_view.code\n");
    sql.append("FROM   order_line_item oli\n");
    sql.append("       JOIN order_detail o\n");
    sql.append("         ON o.id = oli.order_id\n");
    sql.append("       JOIN order_line_item_pet olip\n");
    sql.append("         ON olip.order_line_item_id = oli.id\n");
    sql.append("       JOIN pet\n");
    sql.append("         ON pet.id = olip.pet_id\n");
    sql.append("       LEFT JOIN (SELECT olip.order_line_item_id order_line_item_id,\n");
    sql.append("                         olip.prescription_id,\n");
    sql.append("                         ps.code\n");
    sql.append("                  FROM   prescription_order_line_item olip\n");
    sql.append("                         JOIN prescription p\n");
    sql.append("                           ON olip.prescription_id = p.id\n");
    sql.append("                         JOIN prescription_status ps\n");
    sql.append("                           ON p.status_id = ps.id) rx_view\n");
    sql.append("         ON rx_view.order_line_item_id = oli.id\n");
    sql.append("WHERE  oli.order_id IN (SELECT o.id\n");
    sql.append("                        FROM   order_detail o\n");
    sql.append("                               JOIN order_line_item oli\n");
    sql.append("                                 ON o.id = oli.order_id\n");
    sql.append("                               JOIN prescription_order_line_item poli\n");
    sql.append("                                 ON oli.id = poli.order_line_item_id\n");
    sql.append("                               JOIN prescription rx\n");
    sql.append("                                 ON rx.id = poli.prescription_id\n");
    sql.append("                        WHERE  rx.id = :prescriptionId)\n");



    SQLQuery query = baseDao.getSession().createSQLQuery(sql.toString());

    query.setLong("prescriptionId", prescriptionId);


    query.setResultTransformer(Transformers.aliasToBean(RelatedPrescriptionOrderLine.class));

    List results = query.list();

    return results;

Thank you!

  • 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-26T19:38:07+00:00Added an answer on May 26, 2026 at 7:38 pm

    You can use addScalar(String columnAlias, Type type) to explicitly alias the columns of your native SQL:

    For example :

     SQLQuery query = baseDao.getSession().createSQLQuery("SELECT o.id as orderId, ps.code as prescriptionStatus...");
     query.setLong("prescriptionId", prescriptionId);
     query.addScalar("orderId",StandardBasicTypes.INTEGER )
     query.addScalar("prescriptionStatus",StandardBasicTypes.STRING )
     query.setResultTransformer(Transformers.aliasToBean(RelatedPrescriptionOrderLine.class))
    
     List results = query.list();
    
     return results;
    

    The transformer will then look for a class called RelatedPrescriptionOrderLine with the setter called setPrescriptionId() , setPrescriptionStatus() etc , and populate the result to its instance via these setters.

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

Sidebar

Related Questions

Does anyone know of a script that can select all text references to URLs
Does anyone know of a way i can extract all jpg images from a
Does anyone know of a DB setting in DotNetNuke, where you can configure ALL
Does anyone know how can I replace the comment_author_url field from all comments by
Does anyone know how can I close all popup windows (window popup by javascript)
Hello All Does anyone know if you can have a deferred GridSplitter resize in
Does anyone know how can I all data rollback in .net c# if one
Does anyone know how can I get the value from all the <option> with
Does anyone know how can I get all values in the select by Jquery?
Does anyone know where I can find a pregenerated database that has all zip

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.