开发者

How can I remove an item from a Hashmap in Hibernate?

I try to delete an item from a hash map with hibernate.

Here is my config on the collection:

@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@OneToMany(mappedBy = "game", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@Where(clause = "charactType='charact'")
@MapKey(name = "shortcut")
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
public Map<String, Characteristic> getCharacteristics()
{
    return characteristics;
}

public void setCharacteristics(Map<String, Characteristic> characteristics)
{
    this.characteristics = characteristics;
}

and here is my remove function on the same object:

@Transactional
public void removeCharacteristic(Characteristic charact)
{
    // getCharacteristics().size();

    getCharacteristics().remove(charact.getShortcut());
}

Using the removeCharacteristic do not delete the item in database. If I uncomment the line to get the size of the list (which force load of the collection), the record is well deleted.

What is the problem ? how can I achieve it without forcing the load of the entire collection ?

开发者_开发技巧EDIT: I replace the map by a List, and it runs like a charm (without loading it previously by the size() function)... This is very strange... So my problem is solved with the list, but I'm curious to know why it does not run ?


Try explicitly setting the collection. e.g.

c = getCharacteristics();
c.remove(...);
setCharacteristics(c); 


Since you said that it works when you use a list instead, is it possible that you are incorrectly using the .remove() function of the map?

The .remove() function on a map takes the KEY of the object you want to remove, not the actual object.

myMap.remove(MyObject.getKey()); //Or however you would get the key

The .remove() function of a list takes the object that you want to remove;

myList.remove(MyObject); 

Edit: I see that you said that if you uncomment that one line, it works correctly, so this most likely does not apply.


This was an hibernate bug, this is now fixed in newer version

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜