具有相同域类和级联保存的HasOne和HasMany [英] HasOne and HasMany of the same domain class and cascade save

查看:76
本文介绍了具有相同域类和级联保存的HasOne和HasMany的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个领域类;其中最重要的代表一个电子邮件地址,其中包含用户名和电子邮件地址本身.另一类代表一封电子邮件,其中包含主题,发件人(一个电子邮件地址)和收件人(电子邮件地址列表):

I have 2 domain classes; the forst of them represents an email address, with the name of the user and the email address itself. The other class represents an email message, with the subject, the sender (one email address) and the recipients (a list of email addresses):

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

class EmailAddress {
    String name
    String address
}

但是此代码无法正常运行:

But this code doesn't work as I expect:

EmailMessage msg = new EmailMessage()
msg.from = new EmailAddress(name: "User 1", address: "user1@domain.com")
[new EmailAddress(name: "User 2", address: "user2@domain.com"), new EmailAddress(name: "User 3", address: "user3@domain.com")].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]

我什至不知道这是否可以完成,或者我不认为级联保存不起作用.

I don't know if this even can be done, or the cascade save doesn't work as I suppose.

我想我已经在每个类中尝试了hasMany,belongsTo等的所有组合,但没有成功.

I thik I have tried all combinations of hasMany, belongsTo, etc. in each class, without success.

我也阅读了本主题,但是它没有按预期工作 Grails hasOne和具有许多相同的域.

I have also read this topic but it doesn't work as expected Grails hasOne and hasMany same domain.

有人可以帮助我吗?谢谢,谢谢.

Could any one help me? Regards and thanks in advance.

推荐答案

这在标准内存数据库中对我有效

This works for me in the standard memory database

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
}

这篇关于具有相同域类和级联保存的HasOne和HasMany的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆