A Grails 2.3.4
application is connecting to an Oracle database using the following domain class:
class Person {
String name
static mapping = {
id column: "PERSON_ID", generator: "sequence", params: [sequence: 'person_seq']
}
}
The PersonController
makes a call to a method in PersonService
and it makes a call to UtilService
. The method in UtilService
being called has some logic based on wether this Person
object is new:
if (personInstance.id == null) { ... }
What I have found is that the id
property of personInstance
(which is passed through the method calls described above) is assigned when the UtilService
is called.
The controller action calling PersonService
is @Transactional
, and the services do not have any transaction configuration.
So, a couple of questions:
id
value assigned by GORM (I assumed at insert but that seems wrong)?isAttached()
returns true
so that's not good for me)?EDIT: save()
has not been called on the personInstance
when UtilService
does the id
check.
Turns out, I had a findBy
which was flushing the session:
utilService.someMethod(Person.findByUsername(username))
It was at this point that the id
was populated.
Got around it by using withNewTransaction
:
def personInstance = Person.withNewSession { Person.findByUsername(username) }
utilService.someMethod(personInstance)
Which now leads me onto the next question...