Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GRAILS-8815: Domain hasMany association using a List is adding duplicate items when the parent is saved #4446

Closed
graemerocher opened this issue Feb 22, 2012 · 2 comments

Comments

@graemerocher
Copy link
Member

Original Reporter: tednaleid
Environment: mac osx grails 2.0.1
Version: 2.0.1
Migrated From: http://jira.grails.org/browse/GRAILS-8815

If you have an Author that has a List of Book objects:

doublesave.Author:
{code}

package doublesave

class Author {
String firstName
String lastName

// Hi, see here! 
List books

static hasMany = [books: Book]

}
{code}

doublesave.Book:
{code}
package doublesave

class Book {
String title
Date published
BigDecimal price

static belongsTo = [author: Author]

}
{code}

It will re-add a book to the list whenever the Author is saved:

{code}

package doublesave

import grails.test.mixin.*
import org.junit.*

@testfor(Author)
class AuthorTests {

void testSomething() {
    def author = new Author(firstName:'foo', lastName: 'bar')
    author.save()
    assert author.id != null
    assert author.books == null

    def book1 = new Book(title: 'grails', price: 43, published: new Date(), author: author)
    assert author.books == null

    // add the book to the author to complete the other side
    author.addToBooks(book1)
    assert author.books.size() == 1

    author.save()
    // WTF!  Books size is 2 after the author save?!?
    assert author.books.size() == 1

}
}
{code}

The same test fails in unit and integration tests in 2.0.1.

@graemerocher
Copy link
Member Author

graemerocher said:
Well, this is not really a bug, because Grails will automatically link the association, and List allows duplicates.. not sure at this stage

@graemerocher
Copy link
Member Author

graemerocher said:
fixed by spring-attic/grails-data-mapping@48b9f6d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant