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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:54:12+00:00 2026-05-25T14:54:12+00:00

I am having trouble deleting orphan nodes using Hibernate with the following mapping @OneToMany(fetch

  • 0

I am having trouble deleting orphan nodes using Hibernate with the following mapping

@OneToMany(fetch = FetchType.LAZY, mappedBy = "seizure",orphanRemoval=true)
@JsonManagedReference  
@Cascade({CascadeType.ALL,CascadeType.DELETE_ORPHAN})
public Set<SubstanceIdentified> getSubstanceIdentifieds() {
    return this.substanceIdentifieds;
}

the .hbm.xml mapping is like this

  <set name="substanceIdentifieds" table="substance_identified" inverse="true" lazy="true" fetch="select" cascade="all-delete-orphan">
        <key>
            <column name="seizure_id"  not-null="true" />
        </key>
        <one-to-many class="org.unodc.incbszdb.db.base.SubstanceIdentified" />
    </set>

I use Spring MVC and Jackson to do the JSON to Hibernate Class mapping

 @RequestMapping(value = { "/save.json" }, method = RequestMethod.POST)
 public ModelMap save(@RequestBody Seizure seizureObj, Model model) {
    seizureService.saveOrUpdate(seizureObj);

NOTE:

seizureObj has only two NEW entries in its substanceIdentifieds Set.

The id property of seizureObj is set to an existing record in the db. But when I call saveOrUpdate existing records (orphans) don’t get deleted.

Seizure Service uses Spring’s

getHibernateTemplate.saveOrUpdate

I have read the threads about

JPA CascadeType.ALL does not delete orphans
Hibernate deleting orphans when updating collection

It seems my setup is right.

Do I have to

  1. load the according object from the DB in a dummy object object first (with the ID my de-serialized object has)
  2. delete the references to other object
  3. save the changes
  4. update with my de-serialized object

?

  • 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-25T14:54:13+00:00Added an answer on May 25, 2026 at 2:54 pm

    Acutally it is possible to do it without calling clear.

    All I had to do is calling the right function.

    use HibernateDaoSupport.getHibernateTemplate().merge(object)

    In my code I first test if the de-serialized object from jackson
    has a ID attached

    if so I call save, if not I call merge.

    if(obj.getId()){
       myDAO.save(obj);
    }else{
       myDAO.merge(obj);
    }
    

    and the merge function of my DAO is defined like this.

    public void merge(E transientObject) {
      getHibernateTemplate().merge(transientObject);
    }
    

    This deletes the orphans like it is supposed to be.

    If somebody is facing the same problem do not hesitate, I am open to help you with it.

    Regards JS

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

Sidebar

Related Questions

I am having trouble deleting orphan nodes using JPA with the following mapping @OneToMany
I am having some trouble deleting documents from Solr index. I use following code:
I am having trouble deleting this (a specific 'event') from the following javascript object,
having a little trouble with a mapping for the following table setup currently: Shop
Having trouble using libstatgrab -- I receive the following error at compile time: libstatgrabTest.cpp:16:
I'm having trouble deleting images from a mysql database using php. The addition of
Having trouble understanding. With the following css : .bloc .field:nth-last-child(2){ ...some values... } and
Having trouble with the following segment of code. I'm getting a parameter count mismatch.
I'm having some trouble deleting a cookie containing multiple values - the values are
I'm having trouble with deleting my template. My template and destructor: template<class S, class

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.