Search code examples
javahibernateone-to-manysoft-delete

Hibernate soft deletion for child table


Assume we have two entities Customer and AppUser, they are One-To-Many relationship.

Customer Entity:

@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 Billing billing;
    private String name;
    private String address;
    private String zipCode;
    private String city;
    private String state;
    private String notes;
    private char enabled;
    private char deleted;
    private Set appUsers = new HashSet(0);

    //Constructors...

    //Getters and Setters...

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "customer")
    //Filter added to retrieve only records that have not been soft deleted.
    @Where(clause="deleted <> '1'")
    public Set getAppUsers() {
    return this.appUsers;
    }
    public void setAppUsers(Set appUsers) {
    this.appUsers = appUsers;
    }
}

AppUser Entity:

@Entity
@Table(name = "APP_USER")
//Override the default Hibernation delete and set the deleted flag rather than deleting the record from the db.
@SQLDelete(sql="UPDATE app_user SET deleted = '1' WHERE id = ?")
//Filter added to retrieve only records that have not been soft deleted.
@Where(clause="deleted <> '1'")
public class AppUser implements java.io.Serializable {
    private long id;
    private Customer customer;
    private AppRole appRole;
    private char enabled;
    private String username;
    private String appPassword;
    private Date expirationDate;
    private String firstName;
    private String lastName;
    private String email;
    private String phone;
    private String fax;
    private char deleted;
    private Set
    persons = new HashSet(0);

    //Constructors...

    //Getters and Setters...
}

The soft deletion works well for both of them.

My question is that how can I soft delete the AppUser when I remove one item from the set of Customer, then I saveOrUpdate the Customer entity, for example:

Customer customer = getCustomerById(id);
Set<AppUser> appUsers = customer.getAppUsers();

assume now we have four appUsers, then

appUsers.remove(oneItem)

saveCustomer(customer);

Now, the deleted appUser was hard deleted from database, and remain three records. I still want to use soft deletion to handle such case, someone can help on it?


Solution

  • Finally, I found the answer, add @SQLDelete for the collection of Customer class.

    @SQLDelete(sql="UPDATE customer SET deleted = '1' WHERE id = ?")
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "customer")
    //Filter added to retrieve only records that have not been soft deleted.
    @Where(clause="deleted <> '1'")
        public Set getAppUsers() {
        return this.appUsers;
    }
    

    Hibernate doc link: http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#entity-hibspec-filters