Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override default remove()/DELETE in JPA/Hibernate

Tags:

hibernate

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.

like image 773
rynmrtn Avatar asked Jan 18 '23 10:01

rynmrtn


1 Answers

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;
  // ...
}
like image 64
tscho Avatar answered Jan 28 '23 17:01

tscho