lundi 29 septembre 2014

Grails fetch:"join" confusion


Vote count:

0




Recently I upgraded from Grails 1.3.9 to 2.2.4 and noticed some inconsistent behaviour with a fetch:"join" mapping. I created a simple grails project to demonstrate the problem.


Report Domain:



class Report {

String name

static hasMany = [variables:Variable]

static mapping = {
variables cascade: "all-delete-orphan", fetch: "join"
}
}


Variable Domain:



class Variable {

String name

static belongsTo = [parent:Report]
}


Running the following script in Grails console in 2.2.4:



if(!Report.findByName("Test")){
Report report = new Report(name:"Test")
Variable var1 = new Variable(name:"var")
Variable var2 = new Variable(name:"var2")
report.addToVariables(var1)
report.addToVariables(var2)
report.save(flush:true)
}

def report2 = Report.findByName("Test")
println report2.variables

def report3 = Report.findAllByName("Test")
println report3.get(0).variables

def report4 = Report.findByName("Test",[fetch:[variables:"eager"]])
println report4.variables


Gives the following output:


On the first run of the script:



[findbytest.Variable : 1, findbytest.Variable : 2]
[findbytest.Variable : 1, findbytest.Variable : 2]
[findbytest.Variable : 1, findbytest.Variable : 2]


On the second and all subsequent runs of the script:



[findbytest.Variable : 1]
[findbytest.Variable : 1]
[findbytest.Variable : 1]


Removing the fetch:"join" and rerunning the script gives the following output every time:



[findbytest.Variable : 1, findbytest.Variable : 2]
[findbytest.Variable : 1, findbytest.Variable : 2]
[findbytest.Variable : 1, findbytest.Variable : 2]


Running the same script on 1.3.9 with and without the fetch:"join" gives the following output every time:



[findbytest.Variable : 1, findbytest.Variable : 2]
[findbytest.Variable : 1, findbytest.Variable : 2]
[findbytest.Variable : 1, findbytest.Variable : 2]


Any help or direction is appreciated. Thanks!



asked 26 secs ago







Grails fetch:"join" confusion

Aucun commentaire:

Enregistrer un commentaire