2013-08-16 59 views
0

我有2個域類;其中最糟糕的部分代表了一個電子郵件地址,包含用戶名和電子郵件地址本身。其他類表示一封電子郵件,與主題,發件人(一個電子郵件地址)和收件人(電子郵件地址列表):HasOne和HasMany具有相同的域類和級聯保存

class EmailMessage { 
    static hasMany = [ to: EmailAddress ] 
    EmailAddress from 
    String subject 
} 

class EmailAddress { 
    String name 
    String address 
} 

但正如我希望這個代碼不工作:

EmailMessage msg = new EmailMessage() 
msg.from = new EmailAddress(name: "User 1", address: "[email protected]") 
[new EmailAddress(name: "User 2", address: "[email protected]"), new EmailAddress(name: "User 3", address: "[email protected]")].each { 
    msg.addToTo(it) 
} 
msg.subject = "Asunto" 
msg.save(failOnError: true) 

我得到這個錯誤:

| Error 2013-08-14 21:08:40,362 [localhost-startStop-1] ERROR util.JDBCExceptionReporter - La columna "FROM_ID" no permite valores nulos (NULL) 
NULL not allowed for column "FROM_ID"; SQL statement: insert into email_message (id, version, from_id, subject) values (null, ?, ?, ?) [23502-164] 
| Error 2013-08-14 21:08:40,375 [localhost-startStop-1] ERROR context.GrailsContextLoader - Error initializing the application: could not insert: [prueba.EmailMessage]; 
SQL [insert into email_message (id, version, from_id, subject) values (null, ?, ?, ?)]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not insert: [prueba.EmailMessage] 
Message: could not insert: [prueba.EmailMessage]; SQL [insert into email_message (id, version, from_id, subject) values (null, ?, ?, ?)]; constraint [null]; 
nested exception is org.hibernate.exception.ConstraintViolationException: could not insert: [prueba.EmailMessage] 

我不知道這甚至可以做到,或者級聯保存爲我想不工作。

我thik我嘗試過hasMany,belongsTo等每個類的所有組合,沒有成功。

我也讀過這個主題,但它沒有按預期工作Grails hasOne and hasMany same domain

任何人都可以幫助我嗎?提前感謝和感謝。

+0

我不知道這是否是相同的錯誤,但看一看:http://jira.grails.org/browse/GRAILS- 2736 – felipenasc

回答

1

這個工作對我來說在標準內存數據庫

class EmailMessage { 
    static hasMany = [ to: EmailAddress ] 
    EmailAddress from 
    String subject 

    static mapping = { 
     from cascade: "save-update" //Pretty sure this is unnecessary 
    } 
} 

class EmailAddress { 
    String name 
    String address 

    static belongsTo = EmailMessage 
} 
+0

你是對的。只要在我的短期測試中,映射塊不是必需的。其餘的代碼現在很好用。謝謝! – okelet

+0

拋出:'''org.springframework.dao.InvalidDataAccessApiUsageException:非空屬性引用一個瞬態值 - 在當前操作之前必須保存瞬態實例:test.Child.parent - > test.Parent;嵌套的異常是org.hibernate.TransientPropertyValueException:非空屬性引用瞬態值 - 瞬態實例必須在當前操作之前保存:test.Child.parent - > test.Parent'''任何想法如何解決它? – Sachin

+0

http://stackoverflow.com/q/38665175/3580410 – Sachin

1

不幸的是,在保存父項之前,您必須先保存您的from

EmailMessage msg = new EmailMessage() 
msg.from = new EmailAddress(name: "User 1", address: "[email protected]") 
if (!msg.from.save(flush: true)) { 
    log.error("message could not be saved: " + msg.from.errors) 
} 
[new EmailAddress(name: "User 2", address: "[email protected]"), new EmailAddress(name: "User 3", address: "[email protected]")].each { 
    msg.addToTo(it) 
} 
msg.subject = "Asunto" 
msg.save(failOnError: true) 

1:1關係中沒有層疊保存。

+0

您可以級聯保存在1:1 –

相關問題