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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:25:34+00:00 2026-06-10T07:25:34+00:00

So I have an few objects in the domain which are in a hasMany

  • 0

So I have an few objects in the domain which are in a hasMany relation with each other as below

Class Car {
    String name
    SortedSet tires = [] as SortedSet
    static hasMany = [tires: Tire]
}

Class Tire {
    String type
    SortedSet screws = [] as SortedSet
    static hasMany = [screws: Screw]
}

Class Screws {
     String type
}

Now I would like to take the entire object tree offline for a certain kind of car that I can get by a findByName. I know we can do a fetch eager on the finder, but that goes down just one level. As in the example I have 2 levels or more.

So my question is this. Is there an elegant solution to eager fetch the entire object tree and then use it around without grails/Hibernate firing off another query to fetch the details.

I tried the following which seem to have a similar result but are hardly elegant.

The withCriteria solution

def cars = Car.withCriteria {
    tires {
        screws {
            join 'screws'
        }
    }

Also I tried converting the entire tree into JSON and reparsing it, but that seems to be an overkill. I guess basically I am trying to get the whole object tree offline. Any thoughts if this can be done easily or at all?

TIA

  • 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-10T07:25:35+00:00Added an answer on June 10, 2026 at 7:25 am

    Using the mapping closure:

    Class Car {
        String name
        SortedSet tires = [] as SortedSet
        static hasMany = [tires: Tire]
        static mapping = {
            tires lazy: false
        }
    }
    
    Class Tire {
        String type
        SortedSet screws = [] as SortedSet
        static hasMany = [screws: Screw]
        static mapping = {
            screws lazy: false
        }
    }
    
    Class Screws {
         String type
    }
    

    Maybe you should to take the exception like the rule, I mean, you can to configure your domains class to be lazy: false and to call your finder with fetch lazy:

    def cars = Car.findAllByType(type: "Alfa", [fetch: [tires: 'lazy']])
    

    I don’t know if that is a valid option but you can try it.

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

Sidebar

Related Questions

Final goal: Have a few java objects sharing the same base class persisted into
Have a bit of confusion here. I added a few objects from a class
What is the best way to design the Domain objects which can have multi-lingual
I have a domain object on which I want to store a few things
We have a few objects in our domain model with what you would comically
I have a grails project that contains a few domain objects. I am using
I have few NSDate objects which contain values compliant to this format yyy-MM-dd'T'HH:mm:ss.SSS When
I have a few objects in my program that have about 10-15 attributes each.
Explanation: i have few objects and im declaring them inside $(document).ready(). WHY? because in
Suppose you have a few, quite large (100k+) objects in available and can provide

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.