Hibernate – Desire to get the entire object tree in Grails

So I have some objects in the domain, and they have a hasMany relationship, 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 want to take the entire object tree offline to get some kind of car that I can get through findByName. I know we can snatch the viewfinder, but this will only lower one level. In the example In, I have 2 levels or more.

So my question is this. Is there an elegant solution, eager to get the whole object tree, and then use it without grails/Hibernate to trigger another query To get the detailed information.

I tried the following which seems to have similar results, but it is difficult to be elegant.

withCriteria solution

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

In addition, I tried to Convert the tree to JSON and re-analyze it, but this seems to be an overkill. I guess basically I am trying to take the entire object tree offline. Any ideas if this can be done easily or not at all?

TIA

Use 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 take exceptions like a rule, I mean, you can configure your domain class to be lazy: false and use fetch lazy to call your finder:

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

I don’t know if this is A valid option, but you can try it.

So I have some objects in the domain, which have a hasMany relationship between them, as shown below

Class Car {
String name
SortedSet tires = [] as SortedSet
static hasMany = [tires: Tire]
}< br />
Class Tire {
String type
SortedSet screws = [] as SortedSet
static hasMany = [screws: Screw]
}

Class Screws {
String type
}

Now I want to take the entire object tree offline to get some kind of car that I can get through findByName. I know we can snatch the viewfinder, but this will only lower one level. In the example In, I have 2 levels or more.

So my question is this. Is there an elegant solution, eager to get the whole object tree, and then use it without grails/Hibernate to trigger another query To get the detailed information.

I tried the following which seems to have similar results, but it is difficult to be elegant.

withCriteria solution

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

In addition, I tried to Convert the tree to JSON and re-analyze it, but this seems to be an overkill. I guess basically I am trying to take the entire object tree offline. Any ideas if this can be done easily or not at all?

TIA

Use 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 take exceptions like rules, I mean, you can configure your domain class to be lazy: false and use fetch lazy to call your finder:

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

I don’t know if this is a valid option, but you can try it.< /p>

Leave a Comment

Your email address will not be published.