1
我有三個實體:員工(父親),技術員,開發人員。
策略類型已加入。
當我刪除實體技術人員時,表格「技術員」中的一行被刪除,但沒有行從表「員工」中刪除。我如何刪除這兩個。這是正常的嗎?
當我刪除一名技術員時,應該刪除一名員工,對吧?JPA繼承策略JOINED - 爲什麼只刪除孩子?
員工:
@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;
}
}
技術員:
@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;
}
}
開發商:
@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);
}
}
公司:
@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);
}
}
}
編輯:解決
如果沒有實體「公司」和關係「OneToMany」:
- 此簡單示例正常工作。
- 技術人員和員工被正確移除。
問題如下:
實體公司對技術人員列表使用了「OneToMany」。
然後嘗試刪除技術員,但仍指向該公司。
前:
technicianFacade.remove(technician0);//not work
解決方案:
//add
company0.addTechnician (technician0);// add to list
companyFacade.edit(company0);
//remove
company0.removeTechnician (technician0);// remove from list
companyFacade.edit(company0);
謝謝你提醒我看看日誌詹姆斯。 O_o 不,沒有發生錯誤。 – xav56883728 2013-05-15 10:39:11