Unwanted selection of the entire tree of objects in grails

So, I have several objects in the domain that are in hasMany relationship with each other, as shown 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 delete the entire tree of objects offline for a certain type of car, which I can get with findByName. I know that we can make a choice for the seeker, but this is reduced by only one level. As in the example, I have 2 levels or more.

So my question is this. Is there an elegant solution to get the whole tree of objects and then use it without using grails / Hibernate, discarding another request to get the details.

I tried the following, it seems like a similar result, but hardly elegant.

Solution withCriteria

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

I also tried converting the whole tree to JSON and redrawing it, but this seems to be overkill. I think basically I'm trying to get the whole tree of objects offline. Any thoughts if this can be done easily or in general?

TIA

+4
source share
1 answer

Using a 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 accept the exception, as a rule, I mean that you can configure the domain class as lazy: false and call your finder with letch:

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

I don't know if this is a valid option, but you can try.

+3
source

All Articles