withCriteria two level deep association eager fetch grails
class TopLevel {
String name
LevelOne levelOne
}
class LevelOne {
String name
LevelTwo levelTwo
}
class LevelTwo {
String name
}
I'd like to load the entire structure. Searching a开发者_JAVA百科round I found this example, but it didn't work. The "println" generated a query to the LevelTwo table.
def result = TopLevel.withCriteria {
eq('name', 'test')
fetchMode "levelOne", FetchMode.JOIN
levelOne {
fetchMode "levelTwo", FetchMode.JOIN
}
}
println result.levelOne.levelTwo.name
Appreciate any help!
- SteveGot it working. Here's the secret sauce:
def result = TopLevel.withCriteria {
eq('name', 'test')
fetchMode 'levelOne', FetchMode.JOIN
fetchMode 'levelOne.levelTwo', FetchMode.JOIN
fetchMode 'levelOne.levelTwo.levelThree', FetchMode.JOIN
}
精彩评论