1

我有三个实体:员工(父亲)、技术员、开发人员。
策略类型已加入。
当我删除实体 Technician 时,表“Technician”中的一行被删除,但表“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;
}
}

技术员:

@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;
    }
}

抽象外观:

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));
    }
}

}

技术员外观:

@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);
4

1 回答 1

0

JPA 将删除两者。

您确定您使用的是 JOINED 继承,而不是 TABLE_PER_CLASS,请检查您编译和部署的代码。

启用日志记录并检查生成了什么 SQL,是否出现任何错误?

于 2013-05-13T15:42:39.610 回答