我有一个中等复杂的设计,我试图从系统中删除一个“ItemCollection”。该对象属于 Publisher(一对多)并具有 MediaItems(一对多)和一个 Posting(一对一)。Posting 由非拥有多对多中的 2 个其他对象引用。
当我删除 ItemCollection (通过在父级上调用 removeFrom() )时,级联被触发,但我最终在多对多集合上出现外键异常。我尝试从 beforeDelete() 中的其他集合中删除 Posting,虽然我的日志输出告诉我这是有效的,但外键异常仍然发生。
主要层次结构在这里:
class Publisher {
String name
static hasMany = [collections : ItemCollection, defaultTags : MetaTag, feeds : Feed]
static mapping = {
collections cascade: 'all-delete-orphan'
}
}
class ItemCollection {
static hasOne = [posting:Posting]
static hasMany = [items:MediaItem, tags:MetaTag]
static mapping = {
items sort:'collectionOrder', order: 'asc', lazy:false, cascade: "all-delete-orphan"
posting cascade: "all-delete-orphan"
tags lazy:false
}
static belongsTo = [publisher:Publisher]
}
class Posting {
static belongsTo = [itemCollection: ItemCollection]
<< see beforeDelete method below >>
}
Posting 被这两个类引用。
class ItemFeed {
List posts
static hasMany = [posts:Posting, segments:FeedSegment]
static mapping = {
segments cascade: 'all-delete-orphan'
posts lazy:'false'
}
static constraints = {
posts nullable:true
segments nullable:true
}
}
class FeedSegment {
Date postingTime
String s3objname
List posts
static belongsTo = [feed:ItemFeed]
static hasMany = [posts:Posting]
static constraints = {
posts nullable:true
}
}
在我的控制器中,我正在调用:
collectionIds.addAll(params?.collectionIds)
collectionIds.each { id->
log.info("deleting ${id}")
def itemCollectionInstance = ItemCollection.get(id)
def publisher = itemCollectionInstance.publisher
publisher.removeFromCollections(itemCollectionInstance)
publisher.save(flush:true)
}
我在 Posting 中添加了以下 onDelete。
def beforeDelete() {
Posting.withNewSession {
log.info 'trying to delete posting from feeds'
Posting thees = Posting.get(id) /// using "this" seemed to not be reliable..?
log.info 'Deleting: ' + thees
def feedsWithPost = ItemFeed.createCriteria().list{
posts {
eq('id', thees.id)
}
}
def x = feedsWithPost
x.each { feed ->
log.info '#posts: ' + feed.posts.size()
feed.removeFromPosts(thees)
if(feed.validate()) {
feed.save()
} else {
log.warn "Problem with validation " + feed.errors.allErrors.dump()
}
log.info '#posts: ' + feed.posts.size() <---- THIS SEEMS TO WORK. I SEE SIZE GO DOWN
}
// repeated for FeedSegment...
}
}
当我尝试删除 ItemCollection 时,我得到以下堆栈跟踪:
2012-10-02 18:09:21,499 [http-bio-9090-exec-1] INFO feeds.Posting - Deleting: com.broadcastr.feeds.Posting : 2
2012-10-02 18:09:21,507 [http-bio-9090-exec-1] INFO feeds.Posting - found feeds: [<com.broadcastr.feeds.ItemFeed@7592b321 category=Arts & Entertainment city=New York City posts=[com.broadcastr.feeds.Posting : 1, com.broadcastr.feeds.Posting : 2] lastUpdated=2012-10-02 17:27:48.819 segments=[] jsonMetaData=null errors=null id=1 version=2>]
2012-10-02 18:09:21,507 [http-bio-9090-exec-1] INFO feeds.Posting - #posts: 2
2012-10-02 18:09:21,507 [http-bio-9090-exec-1] INFO feeds.Posting - #posts: 1
2012-10-02 18:09:21,508 [http-bio-9090-exec-1] INFO feeds.Posting - found feeds: [<com.broadcastr.feeds.ItemFeed@7592b321 category=Arts & Entertainment city=New York City posts=[com.broadcastr.feeds.Posting : 1] lastUpdated=2012-10-02 17:27:48.819 segments=[] jsonMetaData=null errors=grails.validation.ValidationErrors: 0 errors id=1 version=2>]
2012-10-02 18:09:21,510 [http-bio-9090-exec-1] WARN util.JDBCExceptionReporter - SQL Error: 23503, SQLState: 23503
| Error 2012-10-02 18:09:21,510 [http-bio-9090-exec-1] ERROR util.JDBCExceptionReporter - Referential integrity constraint violation: "FKFD598F4D2EE471: PUBLIC.ITEM_FEED_POSTING FOREIGN KEY(POSTING_ID) REFERENCES PUBLIC.POSTING(ID)"; SQL statement:
delete from posting where id=? and version=? [23503-164]
| Error 2012-10-02 18:09:21,512 [http-bio-9090-exec-1] ERROR events.PatchedDefaultFlushEventListener - Could not synchronize database state with session
Message: could not delete: [com.broadcastr.feeds.Posting#2]
Line | Method
->> 41 | doCall in DebugFilters$_closure1_closure2_closure5
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 195 | doFilter in grails.plugin.cache.web.filter.PageFragmentCachingFilter
| 63 | doFilter in grails.plugin.cache.web.filter.AbstractFilter
| 886 | runTask in java.util.concurrent.ThreadPoolExecutor$Worker
| 908 | run . . in ''
^ 680 | run in java.lang.Thread
Caused by JdbcSQLException: Referential integrity constraint violation: "FKFD598F4D2EE471: PUBLIC.ITEM_FEED_POSTING FOREIGN KEY(POSTING_ID) REFERENCES PUBLIC.POSTING(ID)"; SQL statement:
delete from posting where id=? and version=? [23503-164]
->> 329 | getJdbcSQLException in org.h2.message.DbException
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 169 | get in ''
| 146 | get . . in ''
| 398 | checkRow in org.h2.constraint.ConstraintReferential
| 415 | checkRowRefTable in ''
| 291 | checkRow in ''
| 862 | fireConstraints in org.h2.table.Table
| 879 | fireAfterRow in ''
| 99 | update . in org.h2.command.dml.Delete
| 73 | update in org.h2.command.CommandContainer
| 226 | executeUpdate in org.h2.command.Command
| 143 | executeUpdateInternal in org.h2.jdbc.JdbcPreparedStatement
| 129 | executeUpdate in ''
| 105 | executeUpdate in org.apache.commons.dbcp.DelegatingPreparedStatement
| 41 | doCall . in DebugFilters$_closure1_closure2_closure5
| 195 | doFilter in grails.plugin.cache.web.filter.PageFragmentCachingFilter
| 63 | doFilter in grails.plugin.cache.web.filter.AbstractFilter
| 886 | runTask in java.util.concurrent.ThreadPoolExecutor$Worker
| 908 | run . . in ''
^ 680 | run in java.lang.Thread
| Error 2012-10-02 18:09:21,516 [http-bio-9090-exec-1] ERROR servlet.GrailsDispatcherServlet - HandlerInterceptor.afterCompletion threw exception
Message: could not delete: [com.broadcastr.feeds.Posting#2]; SQL [delete from posting where id=? and version=?]; constraint ["FKFD598F4D2EE471: PUBLIC.ITEM_FEED_POSTING FOREIGN KEY(POSTING_ID) REFERENCES PUBLIC.POSTING(ID)"; SQL statement:
delete from posting where id=? and version=? [23503-164]]; nested exception is org.hibernate.exception.ConstraintViolationException: could not delete: [com.broadcastr.feeds.Posting#2]
Line | Method
->> 41 | doCall in DebugFilters$_closure1_closure2_closure5
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 195 | doFilter in grails.plugin.cache.web.filter.PageFragmentCachingFilter
| 63 | doFilter in grails.plugin.cache.web.filter.AbstractFilter
| 886 | runTask in java.util.concurrent.ThreadPoolExecutor$Worker
| 908 | run . . in ''
^ 680 | run in java.lang.Thread
Caused by ConstraintViolationException: could not delete: [com.broadcastr.feeds.Posting#2]
->> 41 | doCall in DebugFilters$_closure1_closure2_closure5
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 195 | doFilter in grails.plugin.cache.web.filter.PageFragmentCachingFilter
| 63 | doFilter in grails.plugin.cache.web.filter.AbstractFilter
| 886 | runTask in java.util.concurrent.ThreadPoolExecutor$Worker
| 908 | run . . in ''
^ 680 | run in java.lang.Thread
Caused by JdbcSQLException: Referential integrity constraint violation: "FKFD598F4D2EE471: PUBLIC.ITEM_FEED_POSTING FOREIGN KEY(POSTING_ID) REFERENCES PUBLIC.POSTING(ID)"; SQL statement:
delete from posting where id=? and version=? [23503-164]
->> 329 | getJdbcSQLException in org.h2.message.DbException
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 169 | get in ''
| 146 | get . . in ''
| 398 | checkRow in org.h2.constraint.ConstraintReferential
| 415 | checkRowRefTable in ''
| 291 | checkRow in ''
| 862 | fireConstraints in org.h2.table.Table
| 879 | fireAfterRow in ''
| 99 | update . in org.h2.command.dml.Delete
| 73 | update in org.h2.command.CommandContainer
| 226 | executeUpdate in org.h2.command.Command
| 143 | executeUpdateInternal in org.h2.jdbc.JdbcPreparedStatement
| 129 | executeUpdate in ''
| 105 | executeUpdate in org.apache.commons.dbcp.DelegatingPreparedStatement
| 41 | doCall . in DebugFilters$_closure1_closure2_closure5
| 195 | doFilter in grails.plugin.cache.web.filter.PageFragmentCachingFilter
| 63 | doFilter in grails.plugin.cache.web.filter.AbstractFilter
| 886 | runTask in java.util.concurrent.ThreadPoolExecutor$Worker
| 908 | run . . in ''
^ 680 | run in java.lang.Thread