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 390079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:55:46+00:00 2026-05-12T15:55:46+00:00

I have an issue trying to delete using hibernate. When I try to delete

  • 0

I have an issue trying to delete using hibernate. When I try to delete I get an exception saying that children exist and there is a FK violation. I want to delete the children also but the delete doesn’t seem to be cascading. After about a week of trying to fix this issue I read that I should be using HibernateInterceptor to keep the session open so that the children can be loaded. When I try to do this now I get the following error:

Failed to load portlet com.blah.blah.CommunicationsPortlet: java.lang.ClassCastException: $Proxy27 incompatible with com.blah.blah.HibernateCommunicationsDAOImpl

Here is the extract from my mapping file:

<set name="communicationCountries" inverse="true" cascade="all,delete-orphan">
<key column="COM_ID" not-null="true" on-delete="cascade" />
<one-to-many class="com.blah.blah.CommunicationCountry"/>
</set>

Here is an extract from the application context:

<bean id="hibernateCommunicationsDAOImplTarget"
class="com.blah.blah.dao.impl.HibernateCommunicationsDAOImpl">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>

<bean id="hibernateCommunicationsDAOImpl" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target"><ref bean="hibernateCommunicationsDAOImplTarget"/></property>
<property name="proxyInterfaces">
<value>com.blah.blah.dao.CommunicationsDAO</value>
</property>
<property name="interceptorNames">
<list>
<value>hibernateInterceptor</value>
</list>
</property>
</bean>

Here is the method in my DAO:

public void deleteCommunication(Integer id) throws DataAccessException
{
HibernateTemplate hibernate = getHibernateTemplate();
Communication existing = (Communication)hibernate.get(Communication.class, id);
hibernate.initialize( existing.getCommunicationCountries());
hibernate.delete(existing);
} 

I really don’t know what I am doing wrong. I do not have a very complex schema, just one table that results in children (countries). Any ideas what I could do to fix this?

  • 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-05-12T15:55:47+00:00Added an answer on May 12, 2026 at 3:55 pm

    If you simply want to delete parent along with children, you do NOT need to load the children collection. You don’t even need to get() the parent, using load() is enough:

    public void deleteCommunication(Integer id) throws DataAccessException {
      HibernateTemplate hibernate = getHibernateTemplate();
      Communication existing = (Communication) hibernate.load(Communication.class, id);
      hibernate.delete(existing);
    }
    

    This, of course, assumes the appropriate mapping / cascade in place. Set mapping excerpt you’ve shown is OK with exception of <key ... on-delete="cascade"/> setting. It’s rather tricky for several reasons:

    1. Your database must support it and your schema must define it. You’ll get an error if that’s not the case.
    2. You will NOT get any performance improvements with this setting if cascade is set to “ALL” because Hibernate would still load each child entity to check for further associations. You need to set cascade to “save-update” in order for this to work, which then has side effects for parent-child relationship with managed lifecycle.

    I would therefore suggest to remove on-delete="cascade" for now and to not use it in general until you completely understand all the implications.

    HibernateInterceptor you’ve mentioned has nothing to do with all of this (most certainly not with the delete() method as you have it coded); it’s part of open-session-in-view approach to communication with UI layer.

    Here’s how to troubleshoot all of the above:

    1. Get rid of HibernateInterceptor for now.
    2. Write a unit test to create parent / children in one transaction, commit it, delete them in 2nd transaction, commit it.
    3. If (2) doesn’t work, you have a problem with your mapping and / or schema. Post both here and I’ll take a look.
    4. If (2) does work and your delete() method above does not work, you have a problem somewhere in your code where delete() is invoked. Perhaps you’re loading that same Communication instance and overwriting its communicationCountries collection.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

An odd issue that I have been trying to address in a project -
I am trying to debug a strange issue with users that have LogMeIn installed.
I have an issue that is driving me a bit nuts: Using a UserProfileManager
I'm trying to adress the following issue: I have a server side .net application
Ok so I've spent a couple hours trying to resolve this issue and have
We have an issue using the PEAR libraries on Windows from PHP . Pear
I have an issue with using AWK to simply remove a field from a
I am having an issue trying to post a JSON string using dojo.xhrPost to
I am very new to JavaScript. Kindly note that I am trying below issue
We're having an issue trying to chain selectors together. We are trying to get

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.