如何从 Hibernate 的 Hashmap 中删除项目?
我尝试使用休眠从哈希映射中删除一个项目。
这是我在集合上的配置:
@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;
}
这是我在同一对象上的删除功能:
@Transactional
public void removeCharacteristic(Characteristic charact)
{
// getCharacteristics().size();
getCharacteristics().remove(charact.getShortcut());
}
使用removeCharacteristic不会删除数据库中的项目。 如果我取消注释该行以获取列表的大小(这会强制加载集合),则记录将被很好地删除。
问题是什么 ?如何在不强制加载整个集合的情况下实现它?
编辑: 我用列表替换地图,它像一个魅力一样运行(之前没有通过 size() 函数加载它)...这很奇怪...所以我的问题是用列表解决的,但我很好奇知道为什么它不运行吗?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试显式设置集合。
例如
Try explicitly setting the collection.
e.g.
既然您说当您使用列表时它会起作用,那么您是否可能错误地使用了地图的 .remove() 函数?
地图上的 .remove() 函数采用您要删除的对象的 KEY,而不是实际对象。
列表的 .remove() 函数获取您想要删除的对象;
编辑:
我看到您说,如果取消注释这一行,它就会正常工作,因此这很可能不适用。
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.
The .remove() function of a list takes the object that you want to remove;
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