0

親子関係をモデル化しようとしています。

次のドメインクラスがあります。

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)
    }
}

すべて生成を実行しました

ただし、新しい Person を作成しようとすると、次のエラーが発生します。

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

バージョン パラメータはどこで生成する必要がありますか? これは、保存呼び出し中に自動的に生成されるべきだと思いました。

更新: 問題は父/母の関係に関連しているようです。これを削除してビューを再生成すると、要素が正常に永続化されるためです。

4

1 に答える 1

0

この問題は、クラスで双方向の関係を作成しようとしたことに関連しているようです。これは実際には必須ではありません。人→父、人→母という一方向の関係があります。逆数は子供の下で計算されます (これを拡張して、父親も含めるようにします)。

私の最終クラスは次のとおりです。

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)
    }
}

私はまだGrailsに慣れていません。

于 2013-02-18T07:17:07.643 に答える