Estoy siguiendo el ejemplo en Grails in Action. Tengo un problema para entender cómo funciona la función addTo*()
.Grails addTo * aclaración
que tienen un dominio sencillo: el usuario, Post, Etiqueta con las siguientes relaciones:
- usuario 1-a-M Mensaje
- Usuario 1-a-M Tag
- mensaje M-a Tag -M
Cuando ejecuto el siguiente código (primer caso):
1. def user = new User(userId: 'joe', password: 'secret').save()
2. def tagGroovy = new Tag(name: 'groovy')
3. def tagGrails = new Tag(name: 'grails')
4. user.addToTags(tagGroovy)
5. user.addToTags(tagGrails)
6.
7. def groovyPost = new Post(content: 'A groovy post')
8. user.addToPosts(groovyPost)
9. groovyPost.addToTags(tagGroovy)
10.
11. User.get(1).tags.each {println it.id + " " + it.name}
12. User.get(1).posts.each {println it.id + " " + it.content + " " + it.dateCreated}
me sale esto:
null grails
null groovy
null A groovy post null
Y si cambio de las líneas 4, 5 y 8 a:
4. user.addToTags(tagGroovy).save(flush: true)
5. user.addToTags(tagGrails).save(flush: true)
8. user.addToPosts(groovyPost).save(flush: true)
me sale esto:
2 grails
1 groovy
1 A groovy post Tue Nov 22 21:00:00 WET 2011
Puede alguien explicar por qué en el primer caso, las propiedades id
y dateCreated
no se conservan?