Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8641741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:36:27+00:00 2026-06-12T11:36:27+00:00

I have a moderately complex design, and I’m trying to delete an ItemCollection from

  • 0

I have a moderately complex design, and I’m trying to delete an “ItemCollection” from the system. This object belongs to a Publisher (one-to-many) and has MediaItems (one-to-many) and a Posting (one-to-one). The Posting is referenced by 2 other objects in a non-owning many-to-many.

When I delete the ItemCollection (by calling removeFrom() on the parent) the cascade is triggered but I wind up with foreign-key exceptions on the many-to-many collections. I’ve tried removing the Posting from the other collections in beforeDelete() and although my logging output tells me that this is working, the foreign-key exception still occurs.

The main hierarchy is here:

  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 is referenced by these two classes.

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

In my controller I’m calling:

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

And I’ve added the following onDelete to Posting.

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

        }
    }

When I try to delete an ItemCollection I get the following stacktrace:

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:36:29+00:00Added an answer on June 12, 2026 at 11:36 am

    Turns out there was another table being referenced that I wasn’t dealing with. To clarify the above answer – withNewSession is the correct idiom to use here and you dont’ want to save(flush:true) inside of a beforeDelete method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a moderately complex report consisting of a lot of subreports. One particular
I am trying to create a moderately complex web page. This is not something
I have a moderately complex application using POJOs, and now come to migrated it
I have a moderately sized dataset in excel from which I wish to extract
have written this little class, which generates a UUID every time an object of
I'm trying to create a moderately complex query with joins: SELECT `history`.`id`, `parts`.`type_id`, `serialized_parts`.`serial`,
I have a moderately complex layout with several clickable elements. When the user clicks
We have a moderately complex silverlight based application written in Silverlight 2. Eventually we
I have a moderately complex PHP if statement with a few levels. Took me
We have a moderately sized solution, with about 20 projects. In one of them

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.