2011-10-31 193 views
0

我在刪除內容時遇到了一些麻煩,當存在與另一個對象的多對多關係。休眠ManyToMany刪除

要通過陣列每個實體進行刪除我有一個名爲查詢的數組和刪除,我循環並執行刪除,實體,像這樣:

private static final String[] DELETE_CONTENT_QUERY_NAMES = { 
    "Entity1.deleteByContentId", 
    "Entity2.deleteByContentId", 
    "Entity3.deleteByContentId", 
    "Entity4.deleteByContentId", 
    "Entity5.deleteByContentId", 
    "EntityWithManyToMany.deleteByContentId", 
    "Entity7.deleteByContentId", 
    "Content.DeleteByContent" 
}; 

@Override 
@Transactional 
public void deleteContent(Content content) throws Exception{ 
    Map<String, Object> params = new HashMap<String, Object>(); 
    params.put("contentId", content.getContentId()); 
    for (String queryName : DELETE_CONTENT_QUERY_NAMES) { 
     dao.batchDeleteByNamedQuery(queryName, params); 
    } 
} 

當迭代嘗試執行EntityWithManyToMany.deleteByContentId HQL批量查詢,我可以在日誌中看到翻譯後的sql指令,但後面跟着一組插入相同的行,MySQL正確地返回了我的約束錯誤。

這是我的多對多關係對象我試圖刪除:

@Entity 
@Embeddable 
public class CategoryContent implements java.io.Serializable { 

    /*** 
    * Private Declarations 
    */ 
    @EmbeddedId 
    @AttributeOverrides({ 
     @AttributeOverride(name = "contentId", column = @Column(
       name = "CONTENT_ID", 
       nullable = false, precision = 10, scale = 0)), 
     @AttributeOverride(name = "categoryId", column = @Column(
       name = "CATEGORY_ID", 
       nullable = false, precision = 10, scale = 0)) 
    }) 
    @NotNull 
    private CategoryContentId id; 

    @ManyToOne(fetch = FetchType.EAGER, cascade=CascadeType.PERSIST) 
    @JoinColumn(name = "CATEGORY_ID", insertable=false, updatable=false) 
    private Category category; 

    @ManyToOne(fetch = FetchType.EAGER, cascade=CascadeType.PERSIST) 
    @JoinColumn(name = "CONTENT_ID", insertable=false, updatable=false) 
    private Content content; 

    @Column(name = "PRIORITY", nullable = true) 
    private Integer priority; 
} 

你認爲這是可能的問題關注CascadeType的?我該如何解決這個問題?

任何建議表示讚賞!

非常感謝, Davide。

回答

0

你在正確的軌道上 - 嘗試修改您的級聯

cascade = {CascadeType.PERSIST, CascadeType.DELETE} 

這應該工作,因爲你有一個連接表。

+0

不幸的是不工作,hibernate具有相同的行爲 – Davide