Search code examples
grailsgrails-orm

How to set version property on domain object creation?


I'm trying to model a parent/child relationship.

I have the following domain class:

package test
class Person {
Person mother
Person father
String name
static hasOne = [father: Person, mother: Person]
    static constraints = {
        name()
        father(nullable:true)
        mother(nullable:true)
    }   
    def Set<Person> children(){
        return Person.findAllByMother(this)
    }
}

I have performed generate-all

However, if I try to create a new Person, I get the following error:

Message: Parameter "#2" is not set; SQL statement:
insert into person (id, version, father_id, mother_id, name) values (null, ?, ?, ?, ?) [90012-164]
    Line | Method
->>  329 | getJdbcSQLException   in org.h2.message.DbException

Where should the version parameter be generated? I thought that this should be generated auto-magically during the save call.

UPDATE: The issue seems to be related to the father/mother relationship, since removing it and re-generating views means that elements are persisted ok.


Solution

  • The issue seems to be related to the fact that I had tried to create a di-directional relationship in the class. This in fact is not required. There is a uni directional relationship Person -> father and Person -> mother. The inverse is calculated under children (which I shall extend to include father as well.)

    My final class is:

    package test
    class Person {
        int id
        Person mother
        Person father
        String name
        static constraints = {
            name()
            father(nullable:true)
            mother(nullable:true)
        }
    
        def Set<Person> children(){
             return Person.findAllByMother(this)
        }
    }
    

    I'm still new to Grails.