java - Hibernate many-to-one delete only child -
where lot of similar topics still can't handle myself, sorry.
so, have object "bank" parent of "office".
trying delete single office, it, offices of parent bank being deleted.
here's code:
bank
@entity @table(name = "banks") public class bank { public bank() { } public bank(string name) { this.name = name; } @id @column(name = "id") @generatedvalue private int id; @column(name = "name") private string name; @onetomany(mappedby = "bank", cascade = cascadetype.all, fetch = fetchtype.eager, orphanremoval = true) @joincolumn(name = "bank_id") private list<office> officelist;
office
@entity @table(name = "offices") public class office { public office() { } public office(string city, string address, string workinghours, bank bank) { this.city = city; this.address = address; this.workinghours = workinghours; this.bank = bank; } @id @column(name = "id") @generatedvalue private int id; @manytoone(fetch = fetchtype.eager, cascade = cascadetype.remove) @joincolumn(name = "bank_id") private bank bank; @column(name = "city") private string city; @column(name = "address") private string address; @column(name = "working_hours") private string workinghours;
and officedao
@repository @transactional public class officedaoimpl implements officedao { @autowired private sessionfactory sessionfactory; @override public office getbyoffice_id(int office_id) { return (office) sessionfactory.getcurrentsession().get(office.class, office_id); } @override public list<office> getalloffice() { criteria criteria = sessionfactory.getcurrentsession().createcriteria(office.class); return criteria.list(); } @override public int save(office office) { return (integer) sessionfactory.getcurrentsession().save(office); } @override public void update(office office) { sessionfactory.getcurrentsession().merge(office); } @override public void view(office office) { sessionfactory.getcurrentsession().merge(office); } @override public void delete(int office_id) { office s = getbyoffice_id(office_id); system.out.println("trying delete" + office_id); sessionfactory.getcurrentsession().delete(s); } }
hibernate query trace deleting office:
hibernate: delete offices id=? hibernate: delete offices id=? hibernate: delete offices id=? hibernate: delete offices id=? hibernate: delete offices id=? hibernate: delete offices id=? hibernate: delete offices id=? hibernate: delete offices id=? hibernate: delete offices id=? hibernate: delete offices id=? hibernate: delete offices id=? hibernate: delete offices id=? hibernate: delete offices id=? hibernate: delete offices id=? hibernate: delete offices id=? hibernate: delete offices id=? hibernate: delete offices id=? hibernate: delete offices id=? hibernate: delete banks id=?
what doing wrong? advice appreciated.
update
if delete cascade = cascadetype.remove office, hibernate won't send delete request.
i've found issue. looks hibernate can't remove child entity while in parent list, add delete method in office:
s.getbank().getofficelist().remove(s);
and yeah, cascade.remove @ office should not exist.
Comments
Post a Comment