i have 2 tables, enterprises , appliance in unidirectional 1 many association.
when try delete enterprise, deletes corresponding appliances too, expected behavior.
now if try delete appliance, corresponding enterprise gets deleted well!! neither expected nor able figure out how solve this. wish if delete appliance appliance should deleted not, enterprise appliance belongs too!!
enterprise class:
@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 , 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(); } }
appliance class:
@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 , 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(); } }
my controller: 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> = 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: yes, cascade update , delete on foreign keys in mysql database!!!!
please inform me, if need other code.
update: solved future reference anyone:
needed remove @ondelete line , and cascade property @manytoone in appliance class!!
Comments
Post a Comment