I have a domain class with a bunch of non nullable strings. For one of the domain properties I call a custom validator which does a database check. When my original domain object has a field nulled in it, during the custom validate the domain object attempts to flush. This causing a ‘not-null property references a null or transient value’ error. I have my hibernate flush mode set to manual, so I have no idea why it is attempting to flush.
String id
String name
String type
String description
static constraints =
{
id unique: true, nullable:false
type unique:false, nullable: false
name(unique:['type'] nullable: false, blank:false,
validator:{val, obj ->
if(val != null)
{
def result = OtherDomain.findByType(val)
if(result == null)
{
return 'foreignkey'
}
}
})
description unique:false,nullable: false
}
static mapping =
{
table 'track'
id column:'id', type: 'string', generator: 'assigned'
version false
}
There is no other domain modification going on. This is the only domain edited during this transaction.
Grails will normally flush the hibernate session before any query, so
OtherDomain.findByType(val)is causing the flush. You can work around this by doing the query in a separate hibernate session like this: