Search code examples
javahibernatesoft-delete

Logical delete at a common place in hibernate


I am using Spring and Hibernate for my application.

I am only allowing logical delete in my application where I need to set the field isActive=false. Instead of repeating the same field in all the entities, I created a Base Class with the property and getter-setter for 'isActive'.

So, during delete, I invoke the update() method and set the isActive to false.

I am not able to get this working. If any one has any idea, please let me know.

Base Entity

public abstract class BaseEntity<TId extends Serializable> implements IEntity<TId> {

    @Basic
    @Column(name = "IsActive")
    protected boolean isActive;

    public Boolean getIsActive() {
        return isActive;
    }

    public void setIsActive(Boolean isActive) {
        isActive= isActive;
    }
}

Child Entity

@Entity(name="Role")
@Table(schema = "dbo")
public class MyEntity extends BaseEntity {
    //remaining entities
}

Hibernate Util Class

public void remove(TEntity entity) {

    //Note: Enterprise data should be never removed.
    entity.setIsActive(false);
    sessionFactory.getCurrentSession().update(entity);
}

Solution

  • Try to replace the code in setIsActive method with:

    public void setIsActive(Boolean isActive) {
        this.isActive = isActive;
    }
    

    in your code the use of variable name without this could be ambiguos...

    I think you should also add @MappedSuperclass annotation to your abstract class to achieve field inheritance.