1

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);
}
Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
Yadu Krishnan
  • 3,492
  • 5
  • 41
  • 80
  • What is the problem? Do you have an exception? – musiKk Jun 04 '14 at 13:00
  • `tx = session.beginTransaction() entity.setIsActive(false); sessionFactory.getCurrentSession().update(entity); tx.commit();` try to do it in session transaction.This can be one reason why the changes anre not reflecting in db – Deepak Jun 04 '14 at 13:01
  • Nope, but the value is not getting set. Even after setting the .setIsActive(false), the value is showing as true. – Yadu Krishnan Jun 04 '14 at 13:03
  • how you named this is painful >_< naming the field "active", the getter "isActive" or "getActive", and the setter "setActive" would be usual. – Nathan Hughes Jun 04 '14 at 13:30
  • I named the field as isActive and generated the getter and setter through intelij. Thanks for the suggestion, I will change that :) – Yadu Krishnan Jun 04 '14 at 13:36

3 Answers3

3

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.

davioooh
  • 23,742
  • 39
  • 159
  • 250
  • Now, I am able to see the correct value being set in the entity(during debug). However, the value is not getting updated in the database. In the database, it is still showing the previous value. :( – Yadu Krishnan Jun 04 '14 at 13:11
  • Thanks very much @daviooh.. I had tried MappedSuperClass before. But it was not working, so I had removed that. Now after sing this.isActive, the MappedSuperClass fixed the issue.. I had spent a lot of time to resolve this, without use. Thanks for the quick answer :) – Yadu Krishnan Jun 04 '14 at 13:29
  • Probably, one more question: What is the best way to set isActive=false for all the children in MyEntity(where OneToMany mapping is specified) ? – Yadu Krishnan Jun 04 '14 at 13:34
  • Yes, you need to ask another question in SO, that's a different issue. – davioooh Jun 04 '14 at 13:47
  • I have run into another problem. I am getting the error 'SQLGrammarException: Invalid column name 'IsActive'' for SOME of the entities. :( – Yadu Krishnan Jun 05 '14 at 08:59
2

The issue with the proposed solution (which you allude to in your comment to that answer) is that does not handle cascading delete.

An alternative (Hibernate specific, non-JPA) solution might be to use Hibernate's @SQLDelete annotation:

http://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/querysql.html#querysql-cud

I seem to recall however that this Annotation cannot be defined on the Superclass and must be defined on each Entity class.

The problem with Logical delete in general however is that you then have to remember to filter every single query and every single collection mapping to exclude these records.

In my opinion an even better solution is to forget about logical delete altogether. Use Hibernate Envers as an auditing mechanism. You can then recover any deleted records as required.

http://envers.jboss.org/

Alan Hay
  • 22,665
  • 4
  • 56
  • 110
  • If we can't specify it in superclass, then we need to duplicate the isActive field in all the entities, which is what I was trying to avoid. – Yadu Krishnan Jun 05 '14 at 06:59
0

You can use the SQLDelete annotation...

@org.hibernate.annotations.SQLDelete;

//Package name...

//Imports...

@Entity
@Table(name = "CUSTOMER")
//Override the default Hibernation delete and set the deleted flag rather than deleting the record from the db.
@SQLDelete(sql="UPDATE customer SET deleted = '1' WHERE id = ?")
//Filter added to retrieve only records that have not been soft deleted.
@Where(clause="deleted <> '1'")
public class Customer implements java.io.Serializable {
private long id;
...
private char deleted;

Source: http://featurenotbug.com/2009/07/soft-deletes-using-hibernate-annotations/