Grails应用程序中奇怪的Hibernate/Gorm行为

Grails应用程序中奇怪的Hibernate/Gorm行为,hibernate,grails,foreign-keys,relational-database,gorm,Hibernate,Grails,Foreign Keys,Relational Database,Gorm,我有一个非常简单的关系,通常级联删除应该可以工作。我的亲戚是这样的: enum StatusName { PENDING, SENDING, SENT } abstract class Notification { StatusName status = StatusName.PENDING Date dateCreated Date scheduledDate String text = "" User recipient boolea

我有一个非常简单的关系,通常级联删除应该可以工作。我的亲戚是这样的:

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
        }
    }
Collection<Notification> notifications = Notification.findAllByRecipient(greedyUser)
notifications*.delete()
Collection<SelectedChannel> selectedChannels = SelectedChannel.findAllByUser(greedyUser)
selectedChannels.each{sc->
    sc.notifications.each{nt->
        sc.removeFromNotifications(nt)
    }
    sc.delete()
}
}

下面是其他的课程:

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']
    }
}
我想删除给定用户的所有选定频道,因此我执行以下操作:

Collection<SelectedChannel> selectedChannels = SelectedChannel.findAllByUser(greedyUser)
selectedChannels*.delete()
从id=?的选定_通道中删除?和版本=?[23503-164]

即使我删除了所有通知,如下所示:

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
        }
    }
Collection<Notification> notifications = Notification.findAllByRecipient(greedyUser)
notifications*.delete()
Collection<SelectedChannel> selectedChannels = SelectedChannel.findAllByUser(greedyUser)
selectedChannels.each{sc->
    sc.notifications.each{nt->
        sc.removeFromNotifications(nt)
    }
    sc.delete()
}
Collection notifications=Notification.findAllByRecipient(greedyUser)
通知*.delete()
我也有同样的错误


问候语

将此映射添加到SelectedChannel域:

static mapping = {
    notifications cascade: 'all-delete-orphan'
}
并删除所选频道,如下所示:

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
        }
    }
Collection<Notification> notifications = Notification.findAllByRecipient(greedyUser)
notifications*.delete()
Collection<SelectedChannel> selectedChannels = SelectedChannel.findAllByUser(greedyUser)
selectedChannels.each{sc->
    sc.notifications.each{nt->
        sc.removeFromNotifications(nt)
    }
    sc.delete()
}
Collection selectedChannels=SelectedChannel.findAllByUser(greedyUser)
所选频道。每个{sc->
sc.notifications.each{nt->
sc.removeFromNotifications(nt)
}
sc.删除()
}

如果selectedChannels也在
User
NotificationType
域中引用,请先使用方法清除引用。

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

static mapping = {
    notifications cascade: 'all-delete-orphan'
}
并删除所选频道,如下所示:

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
        }
    }
Collection<Notification> notifications = Notification.findAllByRecipient(greedyUser)
notifications*.delete()
Collection<SelectedChannel> selectedChannels = SelectedChannel.findAllByUser(greedyUser)
selectedChannels.each{sc->
    sc.notifications.each{nt->
        sc.removeFromNotifications(nt)
    }
    sc.delete()
}
Collection selectedChannels=SelectedChannel.findAllByUser(greedyUser)
所选频道。每个{sc->
sc.notifications.each{nt->
sc.removeFromNotifications(nt)
}
sc.删除()
}

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

抽象类是否解决了问题?我在代码中找不到任何问题…这是因为您正在删除
SelectedChannel
,并且存在与记录关联的
Notification
。您需要首先删除所选频道的所有通知。如果将
Notification
更改为非抽象类,它会工作吗?是抽象类造成了问题吗?我在代码中找不到任何问题…这是因为您正在删除
SelectedChannel
,并且存在与记录关联的
Notification
。您需要首先删除所选频道的所有通知。如果您将通知更改为非抽象类,它会工作吗?谢谢,但这不会改变任何东西。同样的错误(谢谢,但这不会改变任何事情。同样的错误:(