1

従業員 (父)、技術者、開発者の 3 つのエンティティがあります。
戦略タイプが結合されます。
エンティティ技術者を削除すると、テーブル「技術者」から 1 行が削除されますが、テーブル「従業員」からは行が削除されません。どうすれば両方を削除できますか。これは正常ですか?
技術者を削除すると、従業員も削除されますよね?

従業員:

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

編集済み:解決済み

エンティティ "Company" と関係 "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