JPA继承策略JOINED - 为什么只删除子?

时间:2013-05-13 07:09:54

标签: inheritance jpa

我有三个实体:员工(父亲),技术员,开发人员 策略类型已加入。
当我删除实体技术人员时,表“技术人员”中的一行被删除,但没有从表“员工”中删除行。我怎样才能删除这两个。这是正常的吗? 当我删除技术人员时,应该删除员工,对吗?

员工:

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

1 个答案:

答案 0 :(得分:0)

JPA将删除两者。

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

启用日志记录并检查生成的SQL,是否发生任何错误?