Hibernate: Overwrite sql-delete with inheritace

前端 未结 1 501
谎友^
谎友^ 2020-12-17 02:32

I have an entity A and B extends A and try to have a soft-delete with joined inheritance strategy.

@Entity
@Inheritance(strategy =          


        
相关标签:
1条回答
  • 2020-12-17 03:24

    You'd want to create a DeleteEventListener as such:

    public class SoftDeleteEventListener extends DefaultDeleteEventListener {
    
    private static final long serialVersionUID = 1L;
    
    @Override
    public void onDelete(DeleteEvent event, Set arg1) throws HibernateException {
        Object o = event.getObject();
        if (o instanceof SoftDeletable) {
            ((SoftDeletable)o).setStatusId(1);
            EntityPersister persister = event.getSession().getEntityPersister( event.getEntityName(), o);
            EntityEntry entityEntry = event.getSession().getPersistenceContext().getEntry(o);
            cascadeBeforeDelete(event.getSession(), persister, o, entityEntry, arg1);
    
            cascadeAfterDelete(event.getSession(), persister, o, arg1);
    
        } else {
            super.onDelete(event, arg1);
        }
    }
    
    }
    

    hook it into your persistence.xml like this

    <property name = "hibernate.ejb.event.delete" value = "org.something.SoftDeleteEventListener"/> 
    

    Also, don't forget to update your cascades in your annotations.

    0 讨论(0)
提交回复
热议问题