我无法删除子实体(SolicitudArxiu),其中父级(Solicitud)被定义为Cascade,我们可以在其中看到。
似乎父级(级联)实体中关系的定义会影响从子级删除,因为当我删除级联时,删除就会起作用。
是否有表格强制删除孩子(SolicitudArxiu)?
SolicitudArxiuManager(要删除的子实体)
public void delete(int id) {
try {
SolicitudArxiu reference = this.em.getReference(SolicitudArxiu.class, id);
this.em.remove(reference);
} catch (EntityNotFoundException e) {
//we want to remove it
logger.error("Entity not found exeption: ", e);
}
}
Solicitud(父母实体)
@Entity
@Table(name = "t_solicitud")
public class Solicitud implements Serializable {
...
@OneToMany(mappedBy = "solicitud", cascade = CascadeType.ALL, orphanRemoval = true,
fetch = FetchType.EAGER)
private Collection<SolicitudArxiu> sarxius;
...
SolicitudArxiu.java
@Entity
@Table(name = "r_solicitudArxiu",
uniqueConstraints = @UniqueConstraint(columnNames = {"idSolicitud","idArxiu"}))
public class SolicitudArxiu implements Serializable {
...
@ManyToOne
@Basic(optional = false)
@JoinColumn(name = "idSolicitud", referencedColumnName = "id",
foreignKey = @ForeignKey(name = "solicitudArxiu2solicitud"))
private Solicitud solicitud;
@ManyToOne
@Basic(optional = false)
@JoinColumn(name = "idArxiu", referencedColumnName = "id",
foreignKey = @ForeignKey(name = "solicitudArxiu2arxiu"))
private Arxiu arxiu;
...
答案 0 :(得分:0)
如果不从拥有方删除关系,则无法删除该实体,因此请首先删除该关系,然后删除您的孩子。
答案 1 :(得分:0)
我最终决定使用createNativeQuery轻松实现,并避免JPA关系的限制。
em.createNativeQuery("DELETE FROM r_solicitudArxiu WHERE id = ?")
.setParameter(1, id)
.executeUpdate();