Pages

Tuesday, September 10, 2013

JPA / Hibernate - On Delete Set NULL

It's impossible to configure a ON DELETE SET NULL with JPA / Hibernate.

To achieve this, you could define a @PreRemove method that specify the fields to set NULL.

Here's an example:

...

/**
 * The persistent class for the user database table.
 */
@Entity
@Table(name = "`user`")
public class User implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(unique = true, nullable = false)
    private Long id;

    @Column(length = 255)
    private String address;

    @Column(length = 255)
    private String city;

    @Column(nullable = false, length = 2)
    private String country;

    @Column(nullable = false, length = 255)
    private String email;

    @Column(name = "is_active", nullable = false)
    private Boolean isActive;

    @Column(nullable = false, length = 255)
    private String name;

    @Column(nullable = false, length = 40)
    private String password;

    @Column(length = 15)
    private String phone;

    @Column(name = "postal_code", length = 7)
    private String postalCode;

    @Column(nullable = false, length = 2)
    private String province;

    @Column(length = 1)
    private String role;

    @ManyToMany
    @JoinTable(
            schema = "stratageme_ca",
            name = "favourite_product"
            , joinColumns = {
            @JoinColumn(name = "user_id", nullable = false)
    }
            , inverseJoinColumns = {
            @JoinColumn(name = "product_id", nullable = false)
    }
    )
    private List<Product> products;

    @OneToMany(mappedBy = "user")
    private List<PurchaseOrder> purchaseOrders;

    @OneToOne(mappedBy = "user")
    private SavedCart savedCart;

    @OneToMany(mappedBy = "specialist")
    private List<Store> storesSpecialist;


    @OneToMany(mappedBy = "director")
    private List<Store> storesDirector;

    @OneToMany(mappedBy = "vp")
    private List<Store> storesVp;

    @OneToMany(mappedBy = "conseiller")
    private List<Store> storesConseiller;

    @OneToMany(mappedBy = "user")
    private List<Territory> territories;

    @ManyToOne
    @JoinColumn(name = "all_under_banner_id")
    private Banner banner;

    @ManyToOne
    @JoinColumn(name = "title_id")
    private Title title;

    public User() {
    }

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getAddress() {
        return this.address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCity() {
        return this.city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getCountry() {
        return this.country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getEmail() {
        return this.email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Boolean getIsActive() {
        return this.isActive;
    }

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

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPhone() {
        return this.phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getPostalCode() {
        return this.postalCode;
    }

    public void setPostalCode(String postalCode) {
        this.postalCode = postalCode;
    }

    public String getProvince() {
        return this.province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getRole() {
        return this.role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public List<Product> getProducts() {
        return this.products;
    }

    public void setProducts(List<Product> products) {
        this.products = products;
    }

    public List<PurchaseOrder> getPurchaseOrders() {
        return this.purchaseOrders;
    }

    public void setPurchaseOrders(List<PurchaseOrder> purchaseOrders) {
        this.purchaseOrders = purchaseOrders;
    }

    public SavedCart getSavedCart() {
        return this.savedCart;
    }

    public void setSavedCart(SavedCart savedCart) {
        this.savedCart = savedCart;
    }


    public List<Store> getStoresSpecialist() {
        return storesSpecialist;
    }

    public void setStoresSpecialist(List<Store> storesSpecialist) {
        this.storesSpecialist = storesSpecialist;
    }

    public List<Store> getStoresDirector() {
        return storesDirector;
    }

    public void setStoresDirector(List<Store> storesDirector) {
        this.storesDirector = storesDirector;
    }

    public List<Store> getStoresVp() {
        return storesVp;
    }

    public void setStoresVp(List<Store> storesVp) {
        this.storesVp = storesVp;
    }

    public List<Store> getStoresConseiller() {
        return storesConseiller;
    }

    public void setStoresConseiller(List<Store> storesConseiller) {
        this.storesConseiller = storesConseiller;
    }

    public List<Territory> getTerritories() {
        return this.territories;
    }

    public void setTerritories(List<Territory> territories) {
        this.territories = territories;
    }

    public Banner getBanner() {
        return this.banner;
    }

    public void setBanner(Banner banner) {
        this.banner = banner;
    }

    public Title getTitle() {
        return this.title;
    }

    public void setTitle(Title title) {
        this.title = title;
    }


    /**
     * Pre Remove to set ON DELETE SET NULL
     */
    @PreRemove
    public void preRemove(){
        for(Store s : storesConseiller){
            s.setConseiller(null);
        }

        for(Store s : storesSpecialist){
            s.setSpecialist(null);
        }

        for(Store s : storesDirector){
            s.setDirector(null);
        }

        for(Store s : storesVp){
            s.setVp(null);
        }

    }


}

No comments:

Post a Comment