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

Ask A Question

Stats

  • Questions 231k
  • Answers 231k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Thanks for the feedback. Here is what I wound up… May 13, 2026 at 2:07 am
  • Editorial Team
    Editorial Team added an answer The parse error is due to the fact that $1… May 13, 2026 at 2:07 am
  • Editorial Team
    Editorial Team added an answer This seems to work: #! /usr/bin/env ruby module CommonMethods def… May 13, 2026 at 2:07 am

Related Questions

I have an issue whereby the company uses a business tracking system that captures
I am having an issue using the Moq library to mock an Enum within
I am fairly new to ASP.NET and I discovered repeaters recently. Some people use
I'm having a hell of a time with our build scripts right now: I'm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.