2013-05-21 35 views
0

我有一個表自引用領域:Grails的,查詢自引用表

Class Book{ 

    Integer id 
    String name 
    Book version 
} 

當我添加的書沒有「版本」,版本字段爲空 現在我要查詢對於沒有版本記錄書籍表(其版本字段爲空),下面的代碼將無法正常工作:

def results = Book.withCriteria{ 
    eq("version", "null") 
} 

但我發現了此異常:

org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of Book.id 

我應該使用什麼查詢?

+0

順便說一句,這是我得到的錯誤: org.hibernate.PropertyAccessException:發生IllegalArgumentException發生調用get.get – iMiX

回答

3

version是GORM中用於樂觀鎖定的關鍵字。修改您的domaincriteria,如下所示,以使標準返回適當的結果。

//域名

class Book { 
    Integer id 
    String name 
    Book bookVersion 
} 

//標準

def book = new Book(name: "test", version: null) 
book.id = 1 
book.save(flush: true) 

def results = Book.withCriteria{ 
    isNull("bookVersion") 
} 

assert results && results[0] instanceof Book 

還要注意,在bookVersion問題是Book型的,它不能相比Stringnull

+0

getter的感謝,它的工作 – iMiX