3
之前我使用Grails 2.2.2和我創建了一個域類具有一個唯一的字符串屬性:2選擇查詢,而不是一個插入
class Myclass {
String dscr // my String attibute
static constraints = {
dscr unique: true // the code to set it unique
}
}
,然後我運行的Grails控制檯命令來測試這個簡單的類與loggingSql以下代碼=真看所得的查詢:
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 (?, ?)
這裏的奧祕是兩個選擇查詢而不是一個。一個查詢的原因是,我發現here是選擇查詢來檢查唯一性。爲什麼第二個選擇會發生?