|
Hi there,
i have a domain class that modifies a property in the afterInsert event. small example: class Transaction { Long transactionId static constraints = { transactionId nullable: true } def afterInsert() { // copy the record id to transactionId; transactionId = id } } Whenever i save the domain object (transaction.save(flush: true)) in my unit tests all is well and the transactionId is updated. But when i try to find the record using Transaction.findByTransactionId() I get NO results. // do something transaction.save(flush: true) Transaction transaction = Transaction.findByTransactionId(1) // !! no results.. transaction = null I need to do a double save to get things working in order to find something by TransactionId. // do something transaction.save(flush: true) transaction.save(flush: true) Transaction transaction = Transaction.findByTransactionId(1) // !! it works.... The double save seems awkward.. any suggestion on how to eliminate the double save?? /Marco --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
Following up on my own question.
In fact the double save is needed, a better way to do this is just by calling save in the afterInsert. Then the double save is not needed. def afterInsert() { transactionId = id save() // we need to call save to persist the changens made to the object } 2012/6/1 Marco Pas <[hidden email]>: > Hi there, > > i have a domain class that modifies a property in the afterInsert event. > > small example: > > class Transaction { > Long transactionId > > static constraints = { > transactionId nullable: true > } > > def afterInsert() { > // copy the record id to transactionId; > transactionId = id > } > } > > Whenever i save the domain object (transaction.save(flush: true)) in > my unit tests all is well and the transactionId is updated. > But when i try to find the record using > Transaction.findByTransactionId() I get NO results. > > // do something > transaction.save(flush: true) > Transaction transaction = Transaction.findByTransactionId(1) > // !! no results.. transaction = null > > I need to do a double save to get things working in order to find > something by TransactionId. > > // do something > transaction.save(flush: true) > transaction.save(flush: true) > Transaction transaction = Transaction.findByTransactionId(1) > // !! it works.... > > The double save seems awkward.. any suggestion on how to eliminate the > double save?? > > /Marco --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
On 04/06/2012 07:24, Marco Pas wrote:
> Following up on my own question. > > In fact the double save is needed, a better way to do this is just by > calling save in the afterInsert. Then the double save is not needed. > > def afterInsert() { > transactionId = id > save() // we need to call save to persist the changens made to the object > } Semantically that makes perfect sense, as afterInsert fires after the object has been saved to the database. If you make any further changes to the object in afterInsert then you need to save those changes. You might get away without the explicit save() call depending whether you're inside/outside a transaction, etc. but putting one in should be safe in all cases. Ian -- Ian Roberts | Department of Computer Science [hidden email] | University of Sheffield, UK --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
| Powered by Nabble | Edit this page |
