2

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

?

2 Answers 2

2

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

1
  • Merge actually worked!!! No idea why it's called "merge" when it really is deleting the child entry from the DB if the child no longer exists in the parent when calling .merge(parent)
    – Collin
    Jan 28, 2023 at 23:13
1

You should load original object from DB with your object ID, then clear the substanceIdentifieds by calling .clear() on it. Then add your new entries to the same list, so those orphans would get deleted. Because when you load object from DB, based on your collection class hibernate will use org.hibernate.collection.AbstractPersistentCollection implementation classes as wrapper classes for your collection, whose methods monitors collection changes in persistence context. That means your .remove() or .clear() methods identifies the object that needs to be removed from DB when object changes are saved/flushed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.