views:

174

answers:

2

Hello,

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 ?

thanks

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 ?

A: 

Try explicitly setting the collection. e.g.

c = getCharacteristics();
c.remove(...);
setCharacteristics(c);
z5h
No, it does nothing.
Jerome C.
A: 

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.

instanceofTom
yes, you're right but I effectively use the good key to remove the object ;)
Jerome C.