gpt4 book ai didi

chalice 。如何使用嵌套列表保存实体?

转载 作者:行者123 更新时间:2023-12-02 14:11:57 27 4
gpt4 key购买 nike

有以下实体:

class Letter {
String email
String subject
String content

List attachments

static hasMany = [attachments : Attachment]

static constraints = {
subject nullable:true, blank:true
content nullable:true
email email:true
attachments nullable:true
}


class Attachment {
String fileName
Integer size
byte[] data
Letter letter

static belongsTo = [letter:Letter]
static constraints = {
fileName nullable: true, blank: true
size nullable: true, blank: true
data nullable: true, maxSize: 1024*1024*5 /*5MB*/
}

}

在单独的模块中创建一封信,并将附件添加到此列表中。没有坚持。代码:
public Letter createLetterFromMessage(Message message) {
Letter letter = new Letter()

def addr = message.getFrom()[0]
if (addr instanceof InternetAddress) {
def inAddr = addr as InternetAddress
letter.email = inAddr.address
}

letter.subject = message.subject

def part = message as Part
letter.attachments = getAttachmetList(letter,part.getContent())//list of not persisted attaches

return letter
}

在另一个模块中,我需要保存一封已添加附件列表的信件。
代码:
    public void onSuccess(List<Letter> letters) {
for (Letter letter : letters) {
if(letterService.save(letter)){
log.debug "Letter saved with id = ${letter.id}."
//do some work
} else
log.debug "Letter not saved and not added to execution context!"
}
}

这该怎么做?

文档中的示例在这里不起作用。
def book = new Book(title: 'Misery')
author.addToBooks(book)
author.save()

最佳答案

我想如果您只是将字母实例分配给 letter每个附件的属性,然后保存字母实例,它应该将保存传播到每个附件:

letter.attachments.each{ it.letter = letter }
letter.save()

这本质上是 addTo*方法确实

关于 chalice 。如何使用嵌套列表保存实体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21855180/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com