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

  • Home
  • SEARCH
  • 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 371425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:06:48+00:00 2026-05-12T14:06:48+00:00

My Grails app has the following domain objects class ProductType { String name static

  • 0

My Grails app has the following domain objects

class ProductType {
    String name
    static hasMany = [attributes: Attribute]
}

class Attribute {       
    String name
    static belongsTo = [productType: ProductType]
} 

My DB has 7 ProductTypes and each of those has 3 Attributes. If I execute the query:

def results = ProductType.withCriteria {
    fetchMode("attributes", org.hibernate.FetchMode.EAGER)
}

I expect 7 instances of ProductType to be returned, but in fact I get 21 (7 x 3). I understand that if I were to execute an equivalent SQL query to the above, the result set would have 21 rows

prod1 | attr1
prod1 | attr2
prod1 | attr3
..... | .....
..... | .....
prod7 | attr1
prod7 | attr2
prod7 | attr3
-------------
Total 21

But I thought that when I retrieve these results via Hibernate/GORM I should get something more like:

prod1 | attr1, attr2, attr3    
..... | ...................
..... | ...................
prod7 | attr1, attr2, attr3
---------------------------
Total 7

Incidentally, if I remove the eager-loading from the query above, I get 7 ProductTypes as expected. What am I missing?

  • 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-12T14:06:48+00:00Added an answer on May 12, 2026 at 2:06 pm

    you should read this faq: Hibernate does not return distinct results for a query with outer join fetching enabled for a collection (even if I use the distinct keyword)?

    When you specify eager loading, the resultset contains, as you noticed, 7*3 rows but in fact you only have 7 productTypes objects in memory (& 2 extra references for each).
    To do what you want, you can add (be aware that the underlying sql query did not change):

    SetResultTransformer(new DistinctRootEntityResultTransformer())

    def results = ProductType.withCriteria {
        fetchMode("attributes", org.hibernate.FetchMode.EAGER)
        SetResultTransformer(new DistinctRootEntityResultTransformer())
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 259k
  • Answers 259k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer not possible, since there's no "static method" in ColdFusion. The… May 13, 2026 at 11:08 am
  • Editorial Team
    Editorial Team added an answer Is WorkItem in your example mapped with a composite ID?… May 13, 2026 at 11:08 am
  • Editorial Team
    Editorial Team added an answer You'll have to use strcpy() (or similar): ... char c2[100];… May 13, 2026 at 11:08 am

Related Questions

I have a grails app with a domain Restaurant and a domain Person. class
I tried to run the sample application of Nimble 0.2 (blogito) downloaded here but
I'm following Scott Davis' tutorials on developing grails apps, but whenever i try to
I have a Grails app that loads its data from xml files and delivers

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.