Sto seguendo l'esempio in Grails in azione. Ho un problema su come funziona la funzione addTo*()
.Grails addTo * chiarimento
Ho un semplice dominio: utente, Post, Tag con le seguenti relazioni:
- User 1-a-M Messaggio
- User 1-a-M Tag
- post M-to Tag -M
Quando eseguo il seguente codice (primo 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}
ottengo questo:
null grails
null groovy
null A groovy post null
E se cambio le linee 4, 5 e 8 a:
4. user.addToTags(tagGroovy).save(flush: true)
5. user.addToTags(tagGrails).save(flush: true)
8. user.addToPosts(groovyPost).save(flush: true)
ottengo questo:
2 grails
1 groovy
1 A groovy post Tue Nov 22 21:00:00 WET 2011
Qualcuno può spiegare perché nel primo caso le proprietà id
e dateCreated
non vengono mantenute?