春季数据JPA:如何级联删除也是一个id字段的OneToMany子项

时间:2019-10-16 02:51:29

标签: java hibernate jpa spring-data-jpa

我的实体定义如下:

父母:

@Entity
@Table( name = "Case" )
public class Case implements Serializable {

    @Id
    @Size ( max = 40 )
    private String caseid;
    .....
    @OneToMany ( cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true )
    @JoinColumn ( name = "caseid" )
    List< CaseFollowup > followups = new ArrayList<>();
    .....
}

儿童IdClass

public class CaseDetailPK implements Serializable {

    private String caseid;
    private String detailid;
    .....
    @Override
    public boolean equals( Object o ) {
    .....
    }
    @Override
    public int hashCode() {
    .....
    }
}

孩子

@Entity
@Table ( name = "casefollowup" )
@IdClass ( CaseDetailPK .class )
public class CaseFollowup implements Serializable {
    @Id
    @Size ( max = 40 )
    private String caseid;

    @Id
    @Size ( max = 40 )
    private String detailid;
    .....
}

在这里我遇到两个问题:

  1. 当我尝试检索以下情况时:
List<Case> cases = caseRepository.findAll();

cases.getFollowups()返回:

unable to evaluate the expression method threw 'org.hibernate.lazyinitializationexception' exception.
  1. 当我尝试使用caseRepository.deleteAll(cases)删除案例时,出现错误:
org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: NULL not allowed for column "CASEID"; SQL statement:
update casefollowup set caseid=null where adrcaseid=?

没有任何线索来解决它们。有人可以帮我解决这些问题吗?非常感谢。

0 个答案:

没有答案