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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:58:09+00:00 2026-05-25T23:58:09+00:00

We need to get back a collection of Widgets, where each widget can contain

  • 0

We need to get back a collection of Widgets, where each widget can contain zero or more Fidgets. Our problem is that whenever we run from Widget, Hibernate generates one SQL query for each Fidget on each Widget. So If I have one Widget with 19 Fidgets, Hibernate runs 20 SQL queries! I would have expected only two queries: one for the Widget and one for the Fidgets.

We have SQL Server 2005 and WebLogic 10g.

My relevant Spring Configuration:

<jee:jndi-lookup id="dataSourceA" jndi-name="jdbc/dataSourceA" />
<bean id="sessionFactoryA" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSourceA" />
    <property name="mappingResources">
        <list>
            <value>com/mycompany/database/Widget.hbm.xml</value>
            <value>com/mycompany/database/Fidget.hbm.xml</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <value>
            hibernate.query.factory_class=org.hibernate.hql.classic.ClassicQueryTranslatorFactory
            hibernate.dialect=org.hibernate.dialect.SQLServerDialect
            hibernate.show_sql=true
        </value>
    </property>
</bean>
<bean id="transactionManagerA" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactoryA" />
</bean>

My Widget.hbm.xml:

<class name="Widget" package="com.mycompany.database">
    <id name="id" column="ID" type="integer">
        <generator class="identity" />
    </id>
    ...
    <set name="fidgets" table="Fidget" lazy="false">
        <key column="WidgetID" />
        <one-to-many class="Fidget" />
    </set>
</class>

My Fidget.hbm.xml:

<class name="Fidget" package="com.mycompany.database">
    <id name="id" column="ID" type="integer">
        <generator class="identity" />
    </id>
    ...
    <many-to-one name="widget" column="WidgetID" class="Widget" lazy="false" />
</class>

The following SQL is generated by Hibernate for a Widget with four Fidgets and a Widget with six Fidgets:

select fidget_.Col1, fidget_.Col2, ... from Fidget fidget_ where fidget_.WidgetID = 7
select fidget_.Col1, fidget_.Col2, ... from Fidget fidget_ where fidget_.WidgetID = 7
select fidget_.Col1, fidget_.Col2, ... from Fidget fidget_ where fidget_.WidgetID = 7
select fidget_.Col1, fidget_.Col2, ... from Fidget fidget_ where fidget_.WidgetID = 7
select fidget_.Col1, fidget_.Col2, ... from Fidget fidget_ where fidget_.WidgetID = 8
select fidget_.Col1, fidget_.Col2, ... from Fidget fidget_ where fidget_.WidgetID = 8
select fidget_.Col1, fidget_.Col2, ... from Fidget fidget_ where fidget_.WidgetID = 8
select fidget_.Col1, fidget_.Col2, ... from Fidget fidget_ where fidget_.WidgetID = 8
select fidget_.Col1, fidget_.Col2, ... from Fidget fidget_ where fidget_.WidgetID = 8
select fidget_.Col1, fidget_.Col2, ... from Fidget fidget_ where fidget_.WidgetID = 8
  • 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-25T23:58:09+00:00Added an answer on May 25, 2026 at 11:58 pm

    Enable batch fetching in the Hibernate configuration to reduce the number of queries, or execute a query that will load the Widgets and their Fidgets:

    select w from Widget w left join fetch w.fidgets where ...
    

    This is also well described in the reference documentation, which is a great source of information.

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

Sidebar

Related Questions

Well, I need get two values that represents the distance between two full dates-only
The one thing that can still get me is what to return from methods?
I need get it independent if the network connection is active or not. I
I need get position element in table. But function position() returns me to the
Just need get some vals located in application.ini(main ini) in the Controller plugin I
I need get all items these have no categories int? categoryId = null; var
I'm making a xml image gallery. And I need get the path from parent,
I have two tables with a weak relation. I need get a text value
Need to get the 10 word before and 10 words after for the given
Need to get an div with overflow:hidden; not to wrap its children. I need

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.