JPA Inheritance strategy JOINED - why only remove child? -
i have 3 entities: employee(father), technician, developer.
strategy type joined.
when remove entity technician, 1 row table "technician" removed no row removed table "employee". how can remove both. normal?
when delete technician employee should removed, right?
employee:
@entity @table(name = "employee", catalog = "curso") @inheritance(strategy = inheritancetype.joined) public class employee implements java.io.serializable { long id; private string nif; private string name; private string phone; private string email; public employee() { } public employee(string name) { this.name = name; } public employee(string nif, string name, string phone, string email) { this.nif = nif; this.name = name; this.phone = phone; this.email = email; } @id @generatedvalue @column(name = "id", unique = true, nullable = false) public long getid() { return this.id; } public void setid(long id) { this.id = id; } @column(name = "nif", length = 10) public string getnif() { return this.nif; } public void setnif(string nif) { this.nif = nif; } @column(name = "name", nullable = false, length = 50) public string getname() { return this.name; } public void setname(string name) { this.name = name; } @column(name = "phone", length = 20) public string getphone() { return this.phone; } public void setphone(string phone) { this.phone = phone; } @column(name = "email", length = 50) public string getemail() { return this.email; } public void setemail(string email) { this.email = email; } }
technician:
@entity @primarykeyjoincolumn(name="employeeid") public class technician extends employee { @joincolumn(name = "company", referencedcolumnname = "id") @manytoone(optional = false) private company company; private int experienceyears = 0; public int getexperienceyears() { return experienceyears; } public void setexperienceyears(int experienceyears) { this.experienceyears = experienceyears; } }
developer:
@entity @primarykeyjoincolumn(name="employeeid") public class developer extends technician { private string expertlanguajes = null; public string getexpertlanguajes() { return expertlanguajes; } public void setexpertlanguajes(string expertlanguajes) { this.expertlanguajes = expertlanguajes; } }
abstractfacade:
import java.util.list; import javax.persistence.entitymanager; import org.springframework.transaction.annotation.transactional; public abstract class abstractfacade<t> { private class<t> entityclass; public abstractfacade(class<t> entityclass) { this.entityclass = entityclass; } protected abstract entitymanager getentitymanager(); @transactional public void remove(t entity) { if (entity != null) { getentitymanager().remove(getentitymanager().merge(entity)); } } }
technicianfacade:
@stateless @repository public class technicianfacade extends abstractfacade<technician> implements technicianfacadelocal { @persistencecontext private entitymanager em; @override protected entitymanager getentitymanager() { return em; } public technicianfacade() { super(technician.class); } }
company:
@entity @table(name = "company") public class company implements serializable, comparable<company> { private static final long serialversionuid = 1l; @id @generatedvalue(strategy = generationtype.auto) @basic(optional = false) @column(name = "id", unique = true, insertable = false, updatable = false) private long id; @onetomany(cascade = cascadetype.all, mappedby = "company") @lazycollection(lazycollectionoption.false) private list<technician> technicianlist = new arraylist(); public company() { } public long getid() { return id; } public void setid(long id) { this.id = id; } @xmltransient public list<technician> gettechnicianlist() { return technicianlist; } public void settechnicianlist(list<technician> technicianlist) { this.technicianlist = technicianlist; } public void removetechnican(technician technician) { if (technician != null) { technicianlist.remove(technician); technician.setcompany(null); } } public void addtechnician(technician technician) { if (!gettechnicianlist().contains(technician)) { gettechnicianlist().add(technician); if (technician.getcompany() != null) { technician.getcompany().gettechnicianlist().remove(technician); } technician.setcompany(this); } } }
edited: solved
without existence of entity "company" , relationship "onetomany":
-this simple example works properly.
-the technician , employee removed.
problem following:
entity company used "onetomany" list of technicians.
try delete technician still points company.
before:
technicianfacade.remove(technician0);//not work
solution:
//add company0.addtechnician (technician0);// add list companyfacade.edit(company0); //remove company0.removetechnician (technician0);// remove list companyfacade.edit(company0);
jpa remove both.
are sure using joined inheritance, not table_per_class, check compiled , deployed code.
enable logging , check sql generated, errors occur?
Comments
Post a Comment