简体   繁体   中英

How to delete an associate entities through Hibernate

I've two classes and corresponding to them two tables. Please assume that all the getters and setters have been added to the classes.

public class Employee implements Serializable {

//@ManyToOne( cascade = CascadeType.PERSIST, targetEntity = RackEntity.class )
//@Fetch( FetchMode.SELECT )
//@JoinColumn( name = "orgName", referencedColumnName = "orgName", nullable = true )
//private Organization org;

private Long id;
private string empName;

@LazyCollection( LazyCollectionOption.FALSE )
@OneToMany( mappedBy = "emp", cascade = { CascadeType.ALL } )
@Cascade( { org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.DELETE,
        org.hibernate.annotations.CascadeType.MERGE, org.hibernate.annotations.CascadeType.PERSIST } )
private Set<Address> empAddress;
}

and

public class Address implements Serializable {

@ManyToOne( cascade = CascadeType.PERSIST, targetEntity = RackEntity.class )
@Fetch( FetchMode.SELECT )
@JoinColumn( name = "empName", referencedColumnName = "empName", nullable = true )
private Employee emp;

private Long id;
private String street;
private String block;

}

Now when I try to delete Employee entity, it successfully deletes it including the associated Address entities.

public void deleteById(Long id) {
    logger.info("Deleting Employee {}", id);
    Employee entity = (Employee) sessionFactory.getCurrentSession()
            .get(Employee.class, id);
    sessionFactory.getCurrentSession().delete(entity);
    sessionFactory.getCurrentSession().flush();
}

But problem comes when I uncomment the code in the Employee class after introducing another class, Organization along with corresponding table as follows:

public class Organization implements Serializable {

private Long id;
private string orgName;

@LazyCollection( LazyCollectionOption.FALSE )
@OneToMany( mappedBy = "org", cascade = { CascadeType.ALL } )
@Cascade( { org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.DELETE,
        org.hibernate.annotations.CascadeType.MERGE, org.hibernate.annotations.CascadeType.PERSIST } )
private Set<Employee> emps;
}

Here, after populating the DB appropriately, when I try to delete Employee entity using same method, I get the following exception:

org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations)

I guess this is because the Employee entities are still being referred in the Organization entity in the form of the field, emps. I tried to find out the solution but didn't get anything elaborate one though.

So could anyone help me in resolving this Exception error along with concrete justifications ??

My idea would be to try and add orphanRemoval = true (in the OneToMany -Line) to Organization and then just to delete the Employee out of the Set. It should remove the everything nicely.

A bit outdated: https://docs.oracle.com/cd/E19798-01/821-1841/giqxy/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM