SPRING + JPA + HIBERNATE删除子项也会删除父对象

时间:2016-01-29 09:01:43

标签: mysql spring hibernate jpa spring-data-jpa

我有两个表,企业和设备在单向一对多关联。

当我尝试删除任何企业时,它也删除了相应的设备,这是预期的行为。

现在,如果我尝试删除任何设备,它的相应企业也会被删除!! 这既不是预期的,也不是我能弄清楚如何解决这个问题。 我希望如果我删除一个设备只是该设备应该被删除,那么设备所属的企业也是!!

企业级:

@Entity
@Table(name="Enterprises")
public class Enterprises implements Serializable {

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

    @Column(name="EntpName")
    private String entpName;

    @Column(name="ContactPerson")
    private String contactPerson;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="CreatedDate", nullable=false)
    private Date createdDate;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="ModifiedDate")
    private Date modifiedDate;

    public Enterprises() { }

    public Enterprises(Long id) {
        this.id = id;
    }

    public Enterprises(String entpName, String contactPerson) {
        this.entpName = entpName;
        this.contactPerson = contactPerson;
        this.setCreatedDate();
    }

    // Getter and setter methods

    public Long getId() {
        return id;
    }

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

    public String getEntpName() {
        return entpName;
    }

    public void setEntpName(String value) {
        this.entpName = value;
    }

    public String getContactPerson() {
        return contactPerson;
    }

    public void setContactPerson(String value) {
        this.contactPerson = value;
    }

    public Date getCreatedDate() { return createdDate; }

    @PrePersist
    public void setCreatedDate() {
        this.createdDate = new Date();
    }

    public Date getModifiedDate() { return modifiedDate; }

    @PreUpdate
    public void setModifiedDate() {
        this.modifiedDate = new Date();
    }
}

设备类:

@Entity
@Table(name="Appliance")
public class Appliance {

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

    @Column(name="ApplianceName")
    private String AppName;

    @Column(name="Parameter1")
    private String param1;

    @Column(name="Parameter2")
    private String param2;

    @Column(name="Parameter3")
    private String param3;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="CreatedDate", nullable=false)
    private Date createdDate;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="ModifiedDate")
    private Date modifiedDate;


    @ManyToOne(cascade = {CascadeType.REMOVE, CascadeType.REFRESH}, fetch = FetchType.LAZY)
    @JoinColumn(name="Enterprises_id", referencedColumnName = "id")
    @OnDelete(action= OnDeleteAction.CASCADE)
    private Enterprises enterprise;
    public Enterprises getEnterprise() {
        return enterprise;
    }

    public void setEnterprise(Enterprises enterprise) {
        this.enterprise = enterprise;
    }


    // ------------------------
    // PUBLIC METHODS
    // ------------------------

    public Appliance() { }

    public Appliance(Long id) {
        this.id = id;
    }

    public Appliance(String AppName, String param1, String param2, String param3) {
        this.AppName = AppName;
        this.param1 = param1;
        this.param2 = param2;
        this.param3 = param3;
        this.setCreatedDate();
    }

    // Getter and setter methods

    public Long getId() {
        return id;
    }

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

    public String getAppName() {
        return AppName;
    }

    public void setAppName(String value) {
        this.AppName = value;
    }

    public String getparam1() {
        return param1;
    }

    public void setparam1(String value) {
        this.param1 = value;
    }

    public String getparam2() {
        return param2;
    }

    public void setparam2(String value) {
        this.param2 = value;
    }

    public String getparam3() {
        return param3;
    }

    public void setparam3(String value) {
        this.param3 = value;
    }

    public Date getCreatedDate() { return createdDate; }

    @PrePersist
    public void setCreatedDate() {
        this.createdDate = new Date();
    }

    public Date getModifiedDate() { return modifiedDate; }

    @PreUpdate
    public void setModifiedDate() {
        this.modifiedDate = new Date();
    }
}

我的控制器: ApplianceUserController:

@Controller
@RequestMapping("/")
public class ApplianceUserController {

    @Autowired
    private ApplianceRepository appliancerepo;
    @Autowired
    private UserRepository userrepo;


    @RequestMapping(value = "{id}/list", method = RequestMethod.GET)
    @ResponseBody
    public LinkedList<List> listStuff(@PathVariable("id") Enterprises id) {
        List<Appliance> appliances = appliancerepo.findApplianceByEnt_id(id);
        List<Users> users = userrepo.findUsersByEnt_id(id);
        LinkedList<List> together = new LinkedList<List>();
        together.add(appliances);
        together.add(users);
        return together;
    }

    @RequestMapping(value="{idd}/appliance/add" , method = RequestMethod.POST)
    @ResponseBody
    Appliance addAppliance(@PathVariable("idd") Enterprises idd , @RequestBody Appliance appliance) {
        appliance.setEnterprise(idd);
        appliance.setCreatedDate();
        return appliancerepo.save(appliance);
    }

    @RequestMapping(value = "appliance/update/{id}", method = RequestMethod.PUT)
    @ResponseBody
    Appliance updateAppliance(@PathVariable("id") Long id, @RequestBody Appliance appliance) {
        Appliance applianceOld= appliancerepo.findById(id);
        applianceOld.setAppName(appliance.getAppName());
        applianceOld.setparam1(appliance.getparam1());
        applianceOld.setparam2(appliance.getparam2());
        applianceOld.setparam3(appliance.getparam3());
        applianceOld.setModifiedDate();
        return appliancerepo.save(applianceOld);

    }

    @RequestMapping(value = "appliance/delete/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    void deleteAppliance(@PathVariable("id") Long id) {
        appliancerepo.delete(id);
    }

    @RequestMapping(value = "appliance/{id}", method = RequestMethod.GET)
    @ResponseBody
    Appliance getA(@PathVariable("id") Long id) {
        Appliance appliance=appliancerepo.findAppliance(id);
        System.out.println(appliance);
        return appliance;
    }
}

PS:是的,对于MySql数据库中的外键,级联更新和删除是ON !!!!

如果您需要任何其他代码,请通知我。

更新解决 供任何人参考:

需要从Appliance类中删除@OnDelete行以及@ManyToOne的cascade属性!!

0 个答案:

没有答案