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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:47:25+00:00 2026-05-25T12:47:25+00:00

I have a very specific 2 part question, which I really really hope hasn’t

  • 0

I have a very specific 2 part question, which I really really hope hasn’t been asked before:)

First the context

I have some Hibernate beans with lazy associations between them such as:

@Entity
public class SalesData {

   @ManyToOne(optional = false, cascade = CascadeType.ALL, fetch = FetchType.LAZY)
   @JoinColumn(nullable = false)
   public Product getProduct() {
     return product;
   }

}

and then there’s:

@Entity
public class Product {

  @OneToMany(mappedBy = "product", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
  public Set<SoundRecording> getSoundRecordings() {
    return soundRecordings;
  }

}

and finally:

@Entity public class SoundRecording {
}

Now if I make a Criteria query where I add aliases for the SalesData to Product relationship and then for the Product to SoundRecording, like so:

Criteria criteria = session.createCriteria(SalesData.class);
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
criteria.createAlias("product", "p");
criteria.createAlias("p.soundRecordings", "s");

I can see in Hibernate’s log that he generates SQL such as:

select ... from SalesData this_ 
inner join Product p1_ on this_.product_id=p1_.id 
    inner join SoundRecording s5_ on p1_.id=s5_.product_id 
        where ...

when I call criteria.list() which is exactly what I want. However If I do:

salesDatas.getProduct().getSoundRecordings(0);

I can see in the log that Hibernate has made another specific SQL select query to retrive this association:

select ... from SoundRecording soundrecor0_ where soundrecor0_.product_id=?

My question is then:

Is this normal? My goal here is to load in one shot (with one SQL query) all that I need specifically so I can avoid these n+1 selects. I thought using alias is one way to do this, especially since I can clearly see that his generates an inner join between the tables. If I use an explictit fetchMode(Join) then hibernate won’t issue all the extra selects, it will be content with the initial select with many (outter) joins. Why is it then that the same is not true for the alias-generated joins?

  • 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-25T12:47:25+00:00Added an answer on May 25, 2026 at 12:47 pm

    An alias or subcriteria is used to create a join. This join might have two goals:

    1. restrict the set of returned values, by adding restrictions over the joined entities
    2. select the fields of the joined entities

    If you want 1 and don’t want 2, then a fetch is not necessary, and you should thus not call setFetchMode()). If you want 2, then a fetch is necessary.

    Example: you might want to search all the products with an order in the current month, but not be interested at all in the fields of the order. In this case, a fetch for the orders of the product is unnecessary, and would be very costly if some products have hundreds or thousands of orders in the month.

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

Sidebar

Related Questions

I have asked a similar question before, but didn't get very good results. I've
Here's an interesting question that I'm shocked hasn't been asked more often on the
Very specific question but we have some good Ada people here so I would
This is a very specific question. I hope there's someone here with good knowledge
I have a very specific html table construct that seems to reveal a Gecko
I have a particular application that needs to calculate something very specific, and while
I have a very odd issue. When I execute a specific database stored procedure
I have very little experience with ASP.NET and I am doing some self-training before
Rather than giving the very specific case (which I did earlier), let me give
Subjective question, I know, but I am counting outgoing clicks for a very specific

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.