如何从 Hibernate 的 Hashmap 中删除项目?

发布于 2024-08-14 22:06:19 字数 992 浏览 8 评论 0原文

我尝试使用休眠从哈希映射中删除一个项目。

这是我在集合上的配置:

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花心好男孩 2024-08-21 22:06:20

尝试显式设置集合。
例如

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

Try explicitly setting the collection.
e.g.

c = getCharacteristics();
c.remove(...);
setCharacteristics(c); 
苦笑流年记忆 2024-08-21 22:06:20

既然您说当您使用列表时它会起作用,那么您是否可能错误地使用了地图的 .remove() 函数?

地图上的 .remove() 函数采用您要删除的对象的 KEY而不是实际对象

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

列表的 .remove() 函数获取您想要删除的对象

myList.remove(MyObject); 

编辑:
我看到您说,如果取消注释这一行,它就会正常工作,因此这很可能不适用。

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.

流年已逝 2024-08-21 22:06:20

这是一个休眠错误,现已在新版本中修复

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文