1

私は非常に単純な関係を持っており、通常はカスケード削除が機能するはずです。私の関係は次のようになります:

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
        }
    }

}

そしてここで他のクラス:

パッケージ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']
    }
}

特定のユーザーの選択されたすべてのチャネルを削除したいので、次のようにします。

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=?およびversion=?[23503-164]

このようにすべての通知を削除しても:

Collection<Notification> notifications = Notification.findAllByRecipient(greedyUser)
notifications*.delete()

同じエラーが発生します...

ご挨拶

4

1 に答える 1

1

このマッピングクロージャを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()
}

selectedChannelsUserがまたはNotificationTypeDomainでも参照されている場合は、 removeFromメソッドを使用して最初に参照をクリアします。

于 2013-03-13T03:44:05.673 に答える