2012-03-22 58 views
0

我們正在使用Grails和遺留數據庫,我們需要控制如何將ID分配給域對象。在Grails 2.0中爲域對象使用分配的ID

我們曾嘗試:

id column: "sco_id", generator:'assigned' 

,但我們得到的異常:

批量更新從更新返回了意外的行數[0];實際行 count:0;預計:1

我們也試圖創建一個自定義ID生成:

public class ScoIdGenerator implements IdentifierGenerator { 

    public Serializable generate(SessionImplementor session, Object object) { 

     /*Generate ID here*/ 

     return 8; 
    } 

} 

但好像發電機在這種情況下被忽略,所以我們得到的錯誤

DEFAULT keyword cannot be used as column has no DEFAULT 

我不確定這些問題是否特定於Grails 2.

任何幫助表示讚賞?

回答

4

這裏的問題是,我們試圖配置ID與列阻擋

static mapping = { 
    table "table_name" 

    columns { 
     id generator: 'assigned', column: "id_sco", sqlType: "int" 
    } 
} 

相反,我們需要直接配置靜態映射塊內的ID

static mapping = { 
    table "table_name" 

    id generator: 'assigned', column: "id_sco", sqlType: "int" 
    columns { 
     ... 
    } 
} 
+0

感謝您發佈一個詳細的答案。有一天它可能會幫助你的人遇到同樣的問題! – 2012-03-23 08:46:08