2013-11-28 28 views
4

我有兩個類,一個與另一個關聯(這是一對一關係)。他們兩個人共享主密鑰。GORM與一列映射的兩個字段拋出異常... insert =「false」update =「false」

一個屬於另一個(belongsTo),另一個父母(hasOne)。

事情是這樣的:

class Parent { 
    int id 
    static hasOne = [ child : Child ]   
} 

class Child { 
    int id 
    static belongsTo = [ parent: Parent ] 
    static mapping = { 
     parent column: 'id' 
    } 
} 

這是行不通的! :(

回答

6

我找到了答案,該HB錯誤是很清楚,但在格姆你這樣做的方式是不同的。

的代碼將略有改變,只有ID可以改變,而不是爲此的關係,你能夠讓Hibernate(和GORM)你關心插入和更新的領域是哪一個。

注意兒童映射

class Parent { 
    int id 
    static hasOne = [ child : Child ]   
} 

class Child { 
    int id 
    static belongsTo = [ parent: Parent ] 
    static mapping = { 
     parent column: 'id', insertable: false, updateable: false 
    } 
} 

希望這個作品給大家。:)

相關問題