Hibernate는 콜렉션의 중간에서 요소를 제거 할 때 순서가 지정된 콜렉션을 포함하는 객체를 병합시키는 고통을 일으킨다.컬렉션에서 [업데이트, 삭제]의 최대 절전 모드가 DB 제약 조건을 위반합니다. 어떻게 고치는 지?
일부 배경 : 저는 CSV 보고서를 작성하는 도구를 만들고 있습니다. 필자의 모델에서는 보고서 개체에 column.orderIndex int 값에 의해 순서가 지정된 열 개체를 갖기를 원합니다.
orderIndex가 목록에서 마지막이 아닌 열을 제거하면 문제가 발생합니다. 병합()를 실행할 때, Hibernate는 다음 SQL을 실행 : Hibernate가 먼저 삭제를 실행 한 경우
update REPORT_COLUMNS set [...], orderIndex=1 where pkey=2 --This is formerly the 2nd column, and now it's orderIndex is going to collide with the orderIndex of the 1st
delete from REPORT_COLUMNS where pkey=1 --This is the 1st column, which is being removed
이 문제가되지 않을 것입니다.
여기 컬렉션이 선언 방법은 다음과 같습니다
@OneToMany(fetch = FetchType.EAGER)
@Fetch(value = FetchMode.SUBSELECT)
@OrderBy("orderIndex")
@JoinColumn(name = "report", updatable = true, insertable = true, nullable = false)
@Cascade({ org.hibernate.annotations.CascadeType.DELETE,
org.hibernate.annotations.CascadeType.DELETE_ORPHAN,
org.hibernate.annotations.CascadeType.MERGE,
org.hibernate.annotations.CascadeType.PERSIST,
org.hibernate.annotations.CascadeType.REMOVE,
org.hibernate.annotations.CascadeType.SAVE_UPDATE })
public List<ReportColumnImpl> getColumnImpls() {
return columns;
}
당신의 도움이 많이 주시면 감사하겠습니다.
JB - 저는 오라클을 사용하고 있습니다. –