4

Rather than deleting a record, our client would like to mark a record as deleted. We are using JPA2/Hibernate. I'd like to do something like the following:

@Entity
@Table(name="TABLE")
@ActionOverride(action="delete", with="activeFlag = false")
public class Table {
    @Column(name="ACTIVE_FLAG")
    boolean activeFlag;

    // ...

}

I have done this in the past but I can't seem to find the right syntax and annotation.

DataNucleus
  • 15,497
  • 3
  • 32
  • 37
rynmrtn
  • 3,371
  • 5
  • 28
  • 44
  • You could do it with a DeleteEventListener, as described [here][1] [1]: http://stackoverflow.com/questions/5404964/hibernate-overwrite-sql-delete-with-inheritace/10549817#10549817 – slipset May 11 '12 at 10:43

2 Answers2

9

Take a look at the hibernate documentation, the annotation you are looking for is @SQLDelete.

@Entity
@Table(name="TABLE")
@SQLDelete(sql = "UPDATE TABLE SET ACTIVE_FLAG = false WHERE id = ?")
public class Table {
  @Column(name="ACTIVE_FLAG")
  boolean activeFlag;
  // ...
}
tscho
  • 2,024
  • 15
  • 15
  • i am using same `@SQLDelete` but this work when it already inserted but in my case i want to update flag within one thread or (same `@Transactional`) – ankit Aug 09 '18 at 09:32
1

The annotation seems to be org.hibernate.annotations.SQLDelete:

SqlDelete Annotation for overwriting Hibernate default DELETE method

You set your custom update SQL in the sql attribute.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • i am using same `@SQLDelete` but this work when it already inserted but in my case i want to update flag within one thread or (same `@Transactional`) – ankit Aug 09 '18 at 09:38