gpt4 book ai didi

hibernate - 在Grails应用程序中使用奇怪的 hibernate/Gorm行为

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

我有一个非常简单的关系,通常应使用级联删除。我的关系如下所示:

enum StatusName {
PENDING, SENDING, SENT
}

abstract class Notification {
StatusName status = StatusName.PENDING
Date dateCreated
Date scheduledDate
String text = ""
User recipient
boolean hasBeenSeen = false

static belongsTo = [
selectedChannel: SelectedChannel
]

static constraints = {
status blank: false,
inList:[StatusName.PENDING, StatusName.SENDING, StatusName.SENT]
scheduledDate nullable: true
text size: 0..1000
recipient nullable: true
}

def beforeInsert() {
if(!recipient) {
recipient = selectedChannel?.user
}
}

}

这里是其他类(class):

de.carmeq.carmob软件包
class SelectedChannel {

static hasMany = [
notifications: Notification
]

static belongsTo = [
channel: Channel,
user: User,
notificationType: NotificationType
]

static constraints = {
channel blank: false,
user blank: false,
notificationType blank: false, unique: ['channel', 'user']
}
}

我想删除给定用户的所有selectedChannels,所以我执行以下操作:
Collection<SelectedChannel> selectedChannels = SelectedChannel.findAllByUser(greedyUser)
selectedChannels*.delete()

但这会导致以下错误:
Referential integrity constraint violation: "FK237A88EBC25A325D: PUBLIC.NOTIFICATION FOREIGN KEY(SELECTED_CHANNEL_ID) REFERENCES PUBLIC.SELECTED_CHANNEL(ID)"; SQL statement:

从selected_channel删除,其中id =?和版本=? [23503-164]

即使我删除所有这样的通知:
Collection<Notification> notifications = Notification.findAllByRecipient(greedyUser)
notifications*.delete()

我犯了同样的错误...

问候

最佳答案

将此映射闭包添加到 SelectedChannel 域:

static mapping = {
notifications cascade: 'all-delete-orphan'
}

并删除selectedChannels,如下所示:
Collection<SelectedChannel> selectedChannels = SelectedChannel.findAllByUser(greedyUser)
selectedChannels.each{sc->
sc.notifications.each{nt->
sc.removeFromNotifications(nt)
}
sc.delete()
}

如果在 UserNotificationType域中也引用了 selectedChannels ,请首先使用 removeFrom方法清除引用。

关于hibernate - 在Grails应用程序中使用奇怪的 hibernate/Gorm行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15371625/

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