This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix for GRAILS-8815 "Domain hasMany association using a List is addin…
…g duplicate items when the parent is saved"
- Loading branch information
1 parent
b2cb2e8
commit 48b9f6d
Showing
2 changed files
with
68 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...rm-test/src/test/groovy/org/grails/datastore/gorm/AutoLinkOneToManyAssociationSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package org.grails.datastore.gorm | ||
|
||
import spock.lang.Specification | ||
import spock.lang.Issue | ||
import grails.persistence.Entity | ||
import grails.gorm.tests.GormDatastoreSpec | ||
|
||
/** | ||
*/ | ||
class AutoLinkOneToManyAssociationSpec extends GormDatastoreSpec{ | ||
|
||
@Issue('GRAILS-8815') | ||
void "Test that associations are linked automatically when saving"() { | ||
given:"A new domain class with a one-to-many association" | ||
def author = new AutoLinkListAuthor(firstName:'foo', lastName: 'bar') | ||
when:"The domain is saved" | ||
author.save() | ||
then:"The association is intially empty" | ||
author.id != null | ||
author.books == null | ||
|
||
when:"An associated object is added" | ||
def book1 = new AutoLinkListBook(title: 'grails', price: 43, published: new Date(), author: author) | ||
|
||
// add the book to the author to complete the other side | ||
author.addToBooks(book1) | ||
then:"The relationship size is correct" | ||
author.books.size() == 1 | ||
|
||
when:"The domain is saved" | ||
author.save() | ||
then:"The relationship size is still correct" | ||
author.books.size() == 1 | ||
} | ||
|
||
@Override | ||
List getDomainClasses() { | ||
[AutoLinkListAuthor,AutoLinkListBook] | ||
} | ||
|
||
|
||
} | ||
|
||
@Entity | ||
class AutoLinkListAuthor { | ||
Long id | ||
String firstName | ||
String lastName | ||
|
||
// Hi, see here! | ||
List books | ||
|
||
static hasMany = [books: AutoLinkListBook] | ||
} | ||
|
||
@Entity | ||
class AutoLinkListBook { | ||
Long id | ||
String title | ||
Date published | ||
BigDecimal price | ||
|
||
static belongsTo = [author: AutoLinkListAuthor] | ||
} |