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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:22:49+00:00 2026-06-10T04:22:49+00:00

I have a subselect query (like the one below) in my hibernate mapping XML

  • 0

I have a subselect query (like the one below) in my hibernate mapping XML file. The purpose of this is to setup a basic SQL query which will then have various restrictions added via the Criteria API. I am displaying data which can be sorted, filtered, and searched with support for pagenation, and so the Criteria API is very useful for this.

<class name="className" table="tableName">
    <subselect>
        select         x.col1         AS       ID,
                       c.col2         AS       a
                       ....
        from           table1         x,
                       table2         y
        where          x.id           =        y.id
                       ....
                       ....
    </subselect>
    <id name="ID" column="ID"/>
    <property name="a" column="a"/>
    ...
    ...
</class>

and the Java code will then add restrictions by doing something like

Criteria c = sessionFactory.getCurrentSession().createCriteria(tableName.class);
c.add(Restrictions.allEq(m)); // where m is a hashmap containing column filters

However, it seems that the use of subselect to declare the basic query is causing very poor performance in MySQL. Hibernate puts the basic query into a subquery which is in the FROM clause of an outer query which has the added restrictions. So, just to be clear, Hibernate creates a query like A, whereas the query I want is B i.e. I don’t want a subquery as it kills performance.

A:  select _tmp.*                         B: select  t1.col1,
    from (                                           t1.col2 
           select  t1.col1,                  from    table1  t1,
                   t1.col2                           table2  t2 
           from    table1  t1,               where   t1.id = t2.id
                   table2  t2                and     t1.col1 = 'blah' 
           where   t1.id = t2.id             order by t1.col desc 
           ...
          ) _tmp
     where _tmp.col1 = 'blah'
     order by _tmp.col2 desc

So my question is how do I create the basic query in Hibernate without using subselect that allows me to use the Criteria API? What’s the best way to get Hibernate the run query B rather than query A?

I understand that I can create named queries in the mapping file and load them, but that loads a query which, although allows me to add certain restrictions, it isn’t as good as Criteria as it doesn’t allow options for sorting etc …

So are there any other options besides using either

<hibernate-mapping>
 <class name="" table="">
    <subselect></subselect>
 </class>
</hibernate-mapping>

or  
<hibernate-mapping>
  <sql-query name="">   
    <return-scalar column="col" type="string"/>
  </sql-query>
</hibernate-mapping>

as the first option degrades performance through the use of subquery, and the second option doesn’t allow me (to the best of my knowledge anyway?) to use the Criteria API .

  • 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-06-10T04:22:50+00:00Added an answer on June 10, 2026 at 4:22 am

    I have a solution, so I’m adding it here for reference.

    My requirement was to use Hibernate to retrieve data from multiple tables and filter the resultset using the Criteria API. Being fairly new to Hibernate and Criteria, I went for the option I deemed easiest at the time which was to hand-craft some raw SQL to pull the data together and filter that data using Criteria. The use of subselect was the only way I knew at the time to load a hand-crafted SQL statement for futher amendment by Criteria functions. This worked fine in terms of functionality, but as detailed above, performed very poorly when running against MySQL.

    What I didn’t realise I could do with Criteria is use Aliases to place restrictions on associations. So I’ve ditched the raw SQL, and used Hibernate to retrieve my parent data object and used Criteria Aliases to add restrictions on all required associations. This method performs in under 100ms compared to 5 seconds.

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

Sidebar

Related Questions

I have 2 entities User and Role Role.hbm.xml : <?xml version=1.0 encoding=utf-8 ?> <hibernate-mapping
I have a MySQL query that results in something like this: person | some_info
I have a query, that does a LEFT JOIN on a subselect. This query
i have a query like this: SELECT DATEPART(year,some_date), DATEPART(month,some_date), MAX(some_value) max_value FROM some_table GROUP
I have a fairly simple one-to-many type join in a MySQL query. In this
I have Hibernate Entities that look something like this (getters and setters left out):
I have a query that I know can be done using a subselect, but
I've got a sub-select in a query that looks something like this: left outer
I have gone thru some of the links for hibernate fetch strategies on net.One
I have this query: select distinct pdi.height, pdi.width, pj.jobnum , (select count(barcode) from productiondoorinformation

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.