【问题标题】:HasOne and HasMany of the same domain class and cascade saveHasOne 和 HasMany 的同域类和级联保存
【发布时间】:2013-08-16 12:22:32
【问题描述】:

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

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: "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]

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

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

我也读过这个主题,但它没有按预期工作Grails hasOne and hasMany same domain

谁能帮帮我?提前致以问候和感谢。

【问题讨论】:

标签: grails grails-orm


【解决方案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
}

【讨论】:

  • 你是对的。只要在我的简短测试中,映射块就不是必需的。其余的代码现在工作得很好。谢谢!
  • 抛出:org.springframework.dao.InvalidDataAccessApiUsageException: Not-null property references a transient value - transient instance must be saved before current operation : test.Child.parent -> test.Parent; nested exception is org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation : test.Child.parent -> test.Parent 知道如何解决它吗?
【解决方案2】:

很遗憾,您必须先保存您的 from,然后才能保存父级。

EmailMessage msg = new EmailMessage()
msg.from = new EmailAddress(name: "User 1", address: "user1@domain.com")
if (!msg.from.save(flush: true)) {
    log.error("message could not be saved: " + msg.from.errors)
}
[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)

在 1:1 关系中没有级联保存。

【讨论】:

  • 可以1:1级联保存
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-29
  • 2014-07-17
  • 1970-01-01
  • 1970-01-01
  • 2021-11-01
  • 2021-05-28
  • 2012-08-15
相关资源
最近更新 更多