You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you load an object, only add something to an embedded list in that object and do a save, the change to the embedded list is not saved if nothing else on the object changed. However, if you change a simple field on the object as well, then the change in the list is saved. For example:
{code:title=Author.java|borderStyle=solid}
class Author {
String id
String name
List books
Integer meaninglessField = 1
static hasMany = [ books : Book ]
static embedded = [ 'books' ]
}
{code}
{code:title=Book.java|borderStyle=solid}
class Book {
String title
String toString() { title }
}
{code}
{code:title=AuthorController.java|borderStyle=solid}
class AuthorController {
def addBookToAuthor() {
Author author = Author.get(params.id)
Book book = new Book(params)
author.books << book
log.info "in memory instance contains the added book: ${author.books}"
author.save(flush:true)
Author loadedAuthor = Author.get(params.id)
log.info "loading from mongo again shows the new book didn't get saved: ${loadedAuthor.books}"
}
def addBookAndChangeAuthor() {
Author author = Author.get(params.id)
Book book = new Book(params)
author.books << book
author.meaninglessField++
author.save(flush:true)
Author loadedAuthor = Author.get(params.id)
log.info "loading from mongo again shows the new book did get saved when updating another field too: ${loadedAuthor.books}"
}
}
{code}
The text was updated successfully, but these errors were encountered:
Original Reporter: ziftytodd
Environment: Grails 2.0.0.RC1, OS X 10.7.2
Version: 1.0.0.RC1
Migrated From: http:https://jira.grails.org/browse/GPMONGODB-138
If you load an object, only add something to an embedded list in that object and do a save, the change to the embedded list is not saved if nothing else on the object changed. However, if you change a simple field on the object as well, then the change in the list is saved. For example:
{code:title=Author.java|borderStyle=solid}
class Author {
String id
String name
List books
Integer meaninglessField = 1
static hasMany = [ books : Book ]
static embedded = [ 'books' ]
}
{code}
{code:title=Book.java|borderStyle=solid}
class Book {
String title
String toString() { title }
}
{code}
{code:title=AuthorController.java|borderStyle=solid}
class AuthorController {
def addBookToAuthor() {
Author author = Author.get(params.id)
Book book = new Book(params)
author.books << book
log.info "in memory instance contains the added book: ${author.books}"
author.save(flush:true)
}
def addBookAndChangeAuthor() {
Author author = Author.get(params.id)
Book book = new Book(params)
author.books << book
author.meaninglessField++
author.save(flush:true)
}
}
{code}
The text was updated successfully, but these errors were encountered: