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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:50:55+00:00 2026-05-20T10:50:55+00:00

A Product can have 1 or more child Products forming a tree structure. Child

  • 0

A Product can have 1 or more child Products forming a tree structure. Child Product’s will also reference the parent Prdoduct. In my test code I’ve created 1 parent (mortgage) with 2 children (ml and me). Why I try to load the parent Product entity = (Product) hibernateTemplate.get(Product.class, mortgage.getId()); it is using the join correctly.

  1. However, hibernate is also trying to load the children of ml and me even though they both doesn’t have any children. Is there a way to avoid this so that I can reduce the number of queries from 3 to 1.

  2. The SQL generated uses dynamically generated aliases like children0_. Is there a way I can tell hibernate to use my own alias name say ‘p’

Here is my hbm and test code respectively.

hbm

<class name="Product" table="PRODUCT">
        <id name="id" type="java.lang.Long" column="ID">
            <generator class="native">
                <param name="sequence">PRODUCT_SN</param>
            </generator>
        </id>

        <many-to-one name="parent" class="Product" lazy="false" column="PARENT" />

        <set name="children" lazy="false" fetch="join"  table="PRODUCT" cascade="all">
            <key>
                <column name="PARENT" />
            </key>
            <one-to-many class="Product" />
        </set>
        <property name="code" type="java.lang.String" column="CODE" not-null="true" />
        <property name="name" type="java.lang.String" column="NAME" />
        <property name="startDate" type="java.util.Date" column="STARTDATE" />
        <property name="endDate" type="java.util.Date" column="ENDDATE" />
        <property name="decisionable" type="boolean" column="ISDECISIONABLE" />
        <property name="selectable" type="boolean" column="ISSELECTABLE" />
    </class>

Test

public class ProductTest extends HibernateTestCase
{
    @Test
    public void save()
    {
        // Level 1 - mortgage LOB
        Product mortgage = new Product();
        mortgage.setCode("Mortgage");

        Product ml = new Product();
        ml.setCode("Mortgage Loan");

        Product me = new Product();
        me.setCode("Home Equity LOC");

        mortgage.addChild(ml);
        mortgage.addChild(me);

        hibernateTemplate.save(mortgage);
        Product entity = (Product) hibernateTemplate.get(Product.class, mortgage.getId());
    }       
}

LOG

Hibernate: select product0_.ID as ID0_1_, product0_.PARENT as PARENT0_1_, product0_.CODE as CODE0_1_, product0_.NAME as NAME0_1_, product0_.STARTDATE as STARTDATE0_1_, product0_.ENDDATE as ENDDATE0_1_, product0_.ISDECISIONABLE as ISDECISI7_0_1_, product0_.ISSELECTABLE as ISSELECT8_0_1_, children1_.PARENT as PARENT3_, children1_.ID as ID3_, children1_.ID as ID0_0_, children1_.PARENT as PARENT0_0_, children1_.CODE as CODE0_0_, children1_.NAME as NAME0_0_, children1_.STARTDATE as STARTDATE0_0_, children1_.ENDDATE as ENDDATE0_0_, children1_.ISDECISIONABLE as ISDECISI7_0_0_, children1_.ISSELECTABLE as ISSELECT8_0_0_ from PRODUCT product0_ left outer join PRODUCT children1_ on product0_.ID=children1_.PARENT where product0_.ID=?
DEBUG [org.hibernate.type.LongType] binding '1' to parameter: 1

DEBUG [org.hibernate.type.LongType] returning '2' as column: ID0_0_
DEBUG [org.hibernate.type.LongType] returning '1' as column: PARENT0_0_
DEBUG [org.hibernate.type.StringType] returning 'Mortgage Loan' as column: CODE0_0_
DEBUG [org.hibernate.type.StringType] returning null as column: NAME0_0_
DEBUG [org.hibernate.type.TimestampType] returning null as column: STARTDATE0_0_
DEBUG [org.hibernate.type.TimestampType] returning null as column: ENDDATE0_0_
DEBUG [org.hibernate.type.BooleanType] returning 'false' as column: ISDECISI7_0_0_
DEBUG [org.hibernate.type.BooleanType] returning 'false' as column: ISSELECT8_0_0_

DEBUG [org.hibernate.type.LongType] returning null as column: PARENT0_1_
DEBUG [org.hibernate.type.StringType] returning 'Mortgage' as column: CODE0_1_
DEBUG [org.hibernate.type.StringType] returning null as column: NAME0_1_
DEBUG [org.hibernate.type.TimestampType] returning null as column: STARTDATE0_1_
DEBUG [org.hibernate.type.TimestampType] returning null as column: ENDDATE0_1_
DEBUG [org.hibernate.type.BooleanType] returning 'false' as column: ISDECISI7_0_1_
DEBUG [org.hibernate.type.BooleanType] returning 'false' as column: ISSELECT8_0_1_

DEBUG [org.hibernate.type.LongType] returning '1' as column: PARENT3_
DEBUG [org.hibernate.type.LongType] returning '2' as column: ID3_
DEBUG [org.hibernate.type.LongType] returning '3' as column: ID0_0_
DEBUG [org.hibernate.type.LongType] returning '1' as column: PARENT0_0_
DEBUG [org.hibernate.type.StringType] returning 'Home Equity LOC' as column: CODE0_0_
DEBUG [org.hibernate.type.StringType] returning null as column: NAME0_0_
DEBUG [org.hibernate.type.TimestampType] returning null as column: STARTDATE0_0_
DEBUG [org.hibernate.type.TimestampType] returning null as column: ENDDATE0_0_
DEBUG [org.hibernate.type.BooleanType] returning 'false' as column: ISDECISI7_0_0_
DEBUG [org.hibernate.type.BooleanType] returning 'false' as column: ISSELECT8_0_0_
DEBUG [org.hibernate.type.LongType] returning '1' as column: PARENT3_
DEBUG [org.hibernate.type.LongType] returning '3' as column: ID3_

Hibernate: select children0_.PARENT as PARENT1_, children0_.ID as ID1_, children0_.ID as ID0_0_, children0_.PARENT as PARENT0_0_, children0_.CODE as CODE0_0_, children0_.NAME as NAME0_0_, children0_.STARTDATE as STARTDATE0_0_, children0_.ENDDATE as ENDDATE0_0_, children0_.ISDECISIONABLE as ISDECISI7_0_0_, children0_.ISSELECTABLE as ISSELECT8_0_0_ from PRODUCT children0_ where children0_.PARENT=?
DEBUG [org.hibernate.type.LongType] binding '3' to parameter: 1

Hibernate: select children0_.PARENT as PARENT1_, children0_.ID as ID1_, children0_.ID as ID0_0_, children0_.PARENT as PARENT0_0_, children0_.CODE as CODE0_0_, children0_.NAME as NAME0_0_, children0_.STARTDATE as STARTDATE0_0_, children0_.ENDDATE as ENDDATE0_0_, children0_.ISDECISIONABLE as ISDECISI7_0_0_, children0_.ISSELECTABLE as ISSELECT8_0_0_ from PRODUCT children0_ where children0_.PARENT=?
DEBUG [org.hibernate.type.LongType] binding '2' to parameter: 1
  • 1 1 Answer
  • 1 View
  • 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-20T10:50:55+00:00Added an answer on May 20, 2026 at 10:50 am

    You should probably mark the relationship as lazy, so that the children are only loaded when necessary.
    If you want to load a node with its children, use a dedicated query :

    select p from Product left join fetch p.children where p.id = :id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have models Product and Category . Category can have many products. ( Product
I have a table with just product ID's and category ID's (products can be
I have 2 tables, a product table and an image table. Products can have
I have a PRODUCTS table, and each product can have multiple attributes so I
I have problems with faceting. Imagine this situation. Product can be in more than
I have a parent domain class, Product: class Product { ... } and more
I have a products table containing products. Each product can have multiple images. The
I have a product, and a product can have many images. This is through
I have a field 'Description' which can have product descriptions with any unicode characters.
Does anyone have any idea on how can you create a product filtering query

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.