Quantcast

fetchMode eager and update of 1-M collection

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

fetchMode eager and update of 1-M collection

Abhijit Sharma
Hi All,

I have a Author domain class with a 1-M relation with Book domain class.
I have set the fetchMode for the books collection to be eager. When I
add a book to an already existing author (retrieved from the db), and
save it, I get two Author instances with the same id and the same books
(including the new one). If the fetchMode is not set I get only one
updated Author instance and the results are correct. The relevant
snippet is as follows:
-----------------------------------------------
def author = new Author(name: "abc").addToBooks(new Book(name: "def"))
        author.save(flush:true)
        if(author.hasErrors())
            println "hasErrors"

        def results = Author.findAll()
        println "${results.size()} $results"

        author = Author.findByName("abc")
        author.addToBooks(new Book(name: "xyz"))
        author.save(flush:true)
        if(author.hasErrors())
            println "hasErrors"

        results = Author.findAll()
        println "${results.size()} $results"
-----------------------------------------------
and the output is as follows:
-----------------------------------------------
1 [[Author(id = 1)(name = abc)[Book(id = 1)(name = def)]]]
2 [[Author(id = 1)(name = abc)[Book(id = 2)(name = xyz)][Book(id =
1)(name = def)]], [Author(id = 1)(name = abc)[Book(id = 2)(name =
xyz)][Book(id = 1)(name = def)]]]
-----------------------------------------------

Any ideas on what is going on? Am I doing something wrong?

P.S. The detailed classes and the test is pasted below. I am using
grails 1.0.3, groovy 1.5.6.

Regards,
Abhijit


class Author {
    String name
    static hasMany = [books : Book]
    static fetchMode = [books:"eager"]
   
    String toString() {
        def buffer = new StringBuffer()
        buffer.append("[").append(getClass().name)
        buffer.append("(id = ").append(id).append(")")
        buffer.append("(name = ").append(name).append(")")
        books.each {
            buffer.append(it.toString())
        }
        buffer.append("]").toString()
    }
}

class Book {
    String name
    static belongsTo = [author : Author]
    String toString() {
        def buffer = new StringBuffer()
        buffer.append("[").append(getClass().name)
        buffer.append("(id = ").append(id).append(")")
        buffer.append("(name = ").append(name).append(")")
        buffer.append("]").toString()
    }
}

class AuthorTests extends GroovyTestCase {

    void testSomething() {
        def author = new Author(name: "abc").addToBooks(new Book(name:
"def"))
        author.save(flush:true)
        if(author.hasErrors())
            println "hasErrors"

        def results = Author.findAll()
        println "${results.size()} $results"

        author = Author.findByName("abc")
        author.addToBooks(new Book(name: "xyz"))
        author.save(flush:true)
        if(author.hasErrors())
            println "hasErrors"

        results = Author.findAll()
        println "${results.size()} $results"
    }
}

1 [[Author(id = 1)(name = abc)[Book(id = 1)(name = def)]]]
2 [[Author(id = 1)(name = abc)[Book(id = 2)(name = xyz)][Book(id =
1)(name = def)]], [Author(id = 1)(name = abc)[Book(id = 2)(name =
xyz)][Book(id = 1)(name = def)]]]


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Loading...