2013-02-16 46 views
1

我想從嵌入式地圖中刪除條目。如果對象CategoryTag被刪除時,我想在一個攔截器,它從地圖中刪除的條目執行HQL查詢:休眠:HQL從嵌入式地圖中刪除

「產品」型號:

@NotNull 
@ElementCollection 
@CollectionTable(name = "producttag", [email protected](name="id")) 
protected Map<CategoryTag, String> tags = new HashMap<CategoryTag, String>(); 

我有點空白,我怎麼可以編寫HQL查詢。它從問題開始,我不知道如何在刪除查詢中引用地圖。 delete Product.tags t where t.key = :tagProduct.tags is not mapped異常而失敗。

有人可以幫助我嗎?

回答

0

標籤不是實體。

A delete operation only applies to entities of the specified class and its subclasses. It does not cascade to related entities. 

這就是爲什麼你會收到錯誤。正確的方法是加載實體,更新其集合,然後保存該對象。

Product p = session.load(Product.class, id); 
p.removeTags(tag); 
session.flush(); 
+0

'CategoryTag'沒有對'Product'的引用,所以它基本上不知道哪些產品正在使用它。因此,按照您的建議,我必須選擇包含該標籤的所有產品,然後按照您的建議循環刪除它。但是,我想知道是否有一種方法可以用一個簡單的語句來實現。我可以用一個本地查詢來做到這一點:'從producttag刪除tag_KEY =:tag',但是這會導致緩存問題,所以我想知道是否有辦法在HQL中完成它。 – Flo 2013-02-16 12:29:15

+0

我相信你仍然需要按照我告訴你的方式來做,這樣可以幫助你跳過緩存問題。 – 2013-02-16 17:45:24