我有一個應該是許多一對多(雙向)休眠自動刪除許多一對多的關係
這是過濾器類的列表:
@ManyToMany(targetEntity=Serving.class)
@JoinTable(name = "BRIDGE_SERV_FILTER", joinColumns={
@JoinColumn(name="filter_id")}, inverseJoinColumns={@JoinColumn(name="serving_id")})
private List<Serving> servings;
public List<Serving> getServing() { return this.servings; }
public void setServing(List<Serving> servings) { this.servings = servings; }
,這是服務類:
@ManyToMany(targetEntity=Filter.class)
@JoinTable(name = "BRIDGE_SERV_FILTER", joinColumns={
@JoinColumn(name="serving_id")}, inverseJoinColumns={@JoinColumn(name="filter_id")})
private List<Filter> filters;
public List<Filter> getFilters() { return filters;}
public void setFilters(List<Filter> filters) { this.filters = filters;}
那我應該叫什麼從服務獲取所有的過濾器,這是我做(SERV是類型服務):
List<Filter> filters = serv.getFilters();
//iterate from all filter
for(Filter allf : allFilter)
{
boolean mark_this = false;
//..and matched the currently-iterated-all-filters with filter from
//which the serving got
for(com.pos.model.Filter f : filters)
{
//if matched, mark this current filter
if (allf.getId().equals(f.getId()))
mark_this = true;
}
tmodel.addRow(new Object[] { allf.getName(), mark_this });
}
該方法在模型中定義,它是靜態的。所以,當表單加載時,它會調用像jTable.setTableModel(Filter.toTableModel(thisServing));
它讀取並顯示數據。然而,Hibernate也調用刪除,以及:
Hibernate: select filter0_.filter_id as filter1_2_, filter0_.is_showable as is2_2_, filter0_.name as name2_ from DIM_FILTER filter0_ where is_showable=true
Hibernate: select filters0_.serving_id as serving1_4_1_, filters0_.filter_id as filter2_6_1_, filter1_.filter_id as filter1_2_0_, filter1_.is_showable as is2_2_0_, filter1_.name as name2_0_ from BRIDGE_SERV_FILTER filters0_ inner join DIM_FILTER filter1_ on filters0_.filter_id=filter1_.filter_id where filters0_.serving_id=?
Hibernate: delete from BRIDGE_SERV_FILTER where serving_id=?
其實,我沒有做任何刪除所有,我不問冬眠刪除任何內容。我不明白的東西可能會發生......如何防止刪除Hibernate?或者我做錯了什麼?
有趣的是,如果只調用getFilters(),則不會發生任何刪除。但是,如果我開始閱讀過濾器,然後執行addRow(),則會發生刪除。 – user1935622
即使我使用set,仍然會自動刪除一次,我想要'get'。請幫助... – user1935622