私は grails 2.2.2 を使用しており、一意の String 属性を 1 つ持つドメイン クラスを作成しました。
class Myclass {
String dscr // my String attibute
static constraints = {
dscr unique: true // the code to set it unique
}
}
次に、grails コンソール コマンドを実行して、loggingSql = true を指定した次のコードでこの単純なクラスをテストし、結果のクエリを確認します。
def a = new Myclass([dscr:'dscr1'])
a.save()
結果のクエリは次のとおりです。
Hibernate: select this_.id as id0_0_, this_.version as version0_0_, this_.dscr as dscr0_0_ from myclass this_ where this_.dscr=?
Hibernate: select this_.id as id0_0_, this_.version as version0_0_, this_.dscr as dscr0_0_ from myclass this_ where this_.dscr=?
Hibernate: insert into myclass (version, dscr) values (?, ?)
ここで謎なのは、選択クエリが 1 つではなく 2 つあることです。ここで見つけたように、1 つのクエリの理由は、一意性を確認するために選択クエリが作成されるためです。2 番目の選択が発生するのはなぜですか?