2011-08-11 139 views
0

我有2個教學班,@ManyToMany關係時處於休眠連接表刪除項目從刪除實體

一流:剪輯 第二類:位置

主要的想法是,一個夾子可以在幾個可用地點

這是剪輯類。

@Entity 
@Table(name = "CLIP") 
public class Clip { 

    private Long id; 
    private List<Location> locations; 

    @Id @GeneratedValue(strategy=GenerationType.AUTO) 
    public Long getId() { 
     return id; 
    } 
    public void setId(Long id) { 
     this.id = id; 
    } 

    @ManyToMany() 
      @JoinTable(name="CLIP_LOCATION",[email protected](name="CLIP_ID",nullable=false),[email protected](name="LOCATION_ID",nullable=false)) 
    @ForeignKey(name="FK_CLIP_LOCATION",inverseName="FK_LOCATION_CLIP") 
    public List<Location> getLocations() { 
     return locations; 
    } 
    public void setLocations(List<Location> locations) { 
     this.locations = locations; 
} 
} 

這是Location類

@Entity 
@Table(name = "LOCATION") 
public class Location { 
} 

這不是雙向關係

我的問題: 當我從用戶界面刪除的位置,我希望相關行在連接表中自動刪除,

我可以這樣做嗎?

我應該使用什麼註釋?

注意Location類和裁剪類在其他關係中太

感謝 Maayan 我使用了錯誤的關係中使用?

回答

1

這是不可能的。在刪除位置之前,您必須從位置列表中刪除該位置。由於您的關聯是單向的,因此您必須執行查詢以查找位置上可用的所有剪輯,並從每個剪輯中移除位置。

另一種方法是執行本機SQL查詢以從連接表中刪除行,但請記住,在會話中加載的實體不會意識到更改。