JPA

2012-03-03 5 views
1

나는 다음과 같은 기관의지도를 포함하는 개체를 삭제하려고으로 지속지도의 요소를 삭제하는 방법 : 난 항상 StatCategory 테이블에 categories_id에 대해 불평 제약 조건 위반 오류가 발생하지만JPA

@Entity 
public class TrxReport extends Model { 
    @ElementCollection(fetch = FetchType.EAGER) 
    @CollectionTable(name = "StatCategory") 
    @MapKeyColumn(name = "TrxReport_key", nullable = false) 
    @Cascade(value = { CascadeType.ALL }) 
    public Map<String, TrxStatCategory> categories; 

    @Override 
    public TrxReport delete(){ 
     for (AtmPosTrxStatCategory cat : categories.values()) { 
      if (cat != null){ 
       categories.remove(cat.name); 
       cat.delete(); 
      } 
     } 
     super.delete(); 
     return this; 
    } 
} 

. 삭제가 사용자 지정 쿼리로 수행되어야합니까? 아니면 위와 유사하게 수행 할 수 있습니까?

답변

1

제거하려고하는 범주가 CascadeType.ALL 때문에 두 번 제거되기 때문에 오류가 발생했다고 생각합니다. orphanRemoval을 사용하는 경우 은 categories 컬렉션에서 제거 된 경우 TrxReport으로 삭제됩니다.

delete()을 무시하지 않고, orphanRemoval 사용에 대한 방법 :

@Entity 
public class TrxReport extends Model { 

    @CollectionTable(name = "StatCategory") 
    @MapKeyColumn(name = "TrxReport_key", nullable = false) 
    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) 
    public Map<String, TrxStatCategory> categories; 

} 
+0

나는 개체의지도를 이용하여 지속 할 수 있다고 생각하지 않습니다 했나 @OneToMany 주석 – emt14

+0

당신이 [@MapKey] (http로를 사용하여 시도 유무 : //docs.oracle.com/javaee/6/api/javax/persistence/MapKey.html)? – maartencls

+0

일대 다 (one-to-many)로 만들 수 없습니까? 그러면지도를 읽기 전용으로하는 별도의 속성을 가질 수 있습니까? – Zoidberg