logo
down
shadow

hibernate @ManyToOne self reference (unidirectional) cascade delete parent


hibernate @ManyToOne self reference (unidirectional) cascade delete parent

By : Roy Cai
Date : November 22 2020, 09:00 AM
I wish did fix the issue. I don't know why you want to keep the unidirectional relation here, because making it bidirectional would make your life much easier.
First, CascadeType.ALL on the parent mapping in your example means that if you delete the subfolder, its parent will be deleted also. And this will propagate all the way up to the root folder which is probably not what you want.
code :
@ManyToOne
@JoinColumn(name = "parent_folder_id")
protected Folder parentFolder;  

@OneToMany(mappedBy = "parentFolder", cascade = {CascadeType.ALL})
protected Set<Folder> children;  
/* pseudo code */
public void deleteFolder(Folder f) {
    Set<Folder> children = session.createQuery("from Folder where parentFolder.id = :parentId").setParameter("parentId", f.getId()).list();
    for each child {
        deleteFolder(child)
    }
    session.delete(f)
}


Share : facebook icon twitter icon
Hibernate, unidirectional ManyToOne and the desire for a "On Delete Cascade" Feature

Hibernate, unidirectional ManyToOne and the desire for a "On Delete Cascade" Feature


By : TheRecruiter
Date : March 29 2020, 07:55 AM
this will help Based on JB Nizet answer I changed my DAO to have a DeleteOperationListener (My base DAO implementation is based on "Don't Repeat the DAO"[1].) In this way I have a generic solution in case I find myself in the same situation again. The structure looks like this:
code :
public interface GenericDao<T, PK extends Serializable> {
    // CRUD methods

    // delete operation listeners.
    void addDeleteListener(DeleteOperationListener<T, PK> deleteOperationListener);

    public interface DeleteOperationListener<T> {
        void preDelete(T entity);
        void posDelete(T entity);
    }
}
@Override
public void delete(T entityToDelete) {
    notifyPreDelete(entityToDelete);
    this.getHibernateTemplate().delete(entityToDelete);
    notifyPosDelete(entityToDelete);
}
@Service
public class ParentModificationListener
    implements GenericDao.DeleteOperationListener<Parent> {

    private ChildDao childDao;

    @Autowired
    public ParentModificationListener(ChildDao childDao, ParentDao parentDao) {
        this.childDao = childDao;
        parentDao.addDeleteListener(this);
    }

    @Override
    public void preDelete(Parent parent) {
        this.childDao.deleteChildrenFromParent(parent);
    }

    @Override
    public void posDelete(Parent parent) {
        // DO NOTHING
    }
}
JPA/Hibernate - How to cascade delete from an entity that has no reference to the one to cascade to?

JPA/Hibernate - How to cascade delete from an entity that has no reference to the one to cascade to?


By : Deepak Bhola
Date : March 29 2020, 07:55 AM
I hope this helps . You can't cascade the delete automatically in this case, you'll need to implement your own delete.
When you delete a user, first run a query like:
code :
delete from Flag F where F.userId = :userId
Hibernate ManyToOne cascade on Add but not on Delete

Hibernate ManyToOne cascade on Add but not on Delete


By : dudi pitusi
Date : March 29 2020, 07:55 AM
it should still fix some issue If you use session.saveOrUpdate(node) you can use the hibernate annotation @Cascade :
code :
@ManyToOne
@Cascade({CascadeType.SAVE_UPDATE})
@JoinColumn(name="parentkey",referencedColumnName="key")
private Node parent;
Hibernate OneToMany/ManyToOne Delete Cascade

Hibernate OneToMany/ManyToOne Delete Cascade


By : Broseph Stalin
Date : March 29 2020, 07:55 AM
this one helps. I'm trying to create a OneToMany/ManyToOne relation. All is working as expected except the delete operation, meaning, when a OwningSide is deleted, the NonOwningSide is deleted as well. , First change cascade=CascadeType.PERSIST at own side.
code :
public class OwningSide {

  @OneToMany(
      cascade = {CascadeType.PERSIST}, mappedBy = "owningSide")
  @LazyCollection(LazyCollectionOption.FALSE)
  @JsonManagedReference  
  private List<NonOwningSide> nonOwningSide;

}

public class NonOwningSide {

  @ManyToOne(
      cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
  @JoinColumn(name = "owningSide_id")
  @JsonBackReference
  private OwningSide owningSide;

}
/** Cascade all operations */
ALL,

/** Cascade persist operation */
PERSIST,

/** Cascade merge operation */
MERGE,

/** Cascade remove operation */
REMOVE,

/** Cascade refresh operation */
REFRESH
alter table NonOwningSide add constraint constraintName 
foreign key (id) references OwningSide(id) on delete set null
How to cascade delete entities with unidirectional 'ManyToOne' relationship with JPA

How to cascade delete entities with unidirectional 'ManyToOne' relationship with JPA


By : Paulo Sérgio Vieira
Date : March 29 2020, 07:55 AM
it fixes the issue You can use CascadeType.DELETE, however this annotation only applies to the objects in the EntityManager, not the database. You want to be sure that ON DELETE CASCADE is added to the database constraint. To verify, you can configure JPA to generate a ddl file. Take a look at the ddl file, you'll notice that ON DELETE CASCADE is not part of the constraint. Add ON DELETE CASCADE to actual SQL in the ddl file, then update your database schema from the ddl. This will fix your problem .
This link shows how to use ON DELETE CASCADE on for CONSTRAINT in MySQL. You do this on the constraint. You can also do it in a CREATE TABLE or ALTER TABLE statement. It's likely that JPA creates the constraint in an ALTER TABLE statement. Simply add ON DELETE CASCADE to that statement.
shadow
Privacy Policy - Terms - Contact Us © animezone.co