Inheritance JPA继承策略加入-为什么只删除子项?

Inheritance JPA继承策略加入-为什么只删除子项?,inheritance,jpa,Inheritance,Jpa,我有三个实体:员工(父亲)、技术人员、开发人员。 策略类型已加入。 当我删除实体技术人员时,表“技术人员”中的一行将被删除,但表“员工”中的任何行都不会被删除。我怎样才能删除这两个。这正常吗? 当我删除一名技术人员时,应该删除一名员工,对吗 员工: @Entity @Table(name = "Employee", catalog = "curso") @Inheritance(strategy = InheritanceType.JOINED) public class Employee im

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

员工:

@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);
    }
}
    }
   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);
技术员:

@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);
    }
}
    }
   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);
开发者:

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

@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);
    }
}
    }
   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);
解决方案:

@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);
    }
}
    }
   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);
JPA将同时删除这两项

是否确定使用的是联接继承,而不是表\每\类,请检查编译和部署的代码


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

感谢您提醒我查看日志。没有,没有发生错误。