如何使用休眠持久化 HashMap [英] How to persist a HashMap with hibernate

查看:31
本文介绍了如何使用休眠持久化 HashMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我对休眠世界很陌生,似乎遇到了障碍.我需要存储的对象中有一个哈希图.

Hello I am very new to the hibernate world and seem to have hit a roadblock. The object I need to store has a hashmap in it.

private Map<String, SentimentFrequencyCounts> modelData = null;

问题是我永远不需要对这张地图进行搜索、排序或做任何事情我只需要将它与对象一起保存并在加载对象时加载它,所以我希望有一些方法可以让休眠只需将其序列化,然后将其存储在 CLOB 或 BLOB 字段中,但我似乎找不到任何方法来做到这一点.

The thing is I will never need to search, sort or do any thing with this map I just need to save it with the object and load it when the object is loaded, so I was hoping there was some way that hibernate could just serializes it and then store it in a CLOB or BLOB field but I can not seem to find any way to do that.

所以我接下来尝试让hibernate像这样保存它

So I next tried to have hibernate save this like so

    @OneToMany(mappedBy="ngram_data", fetch = FetchType.EAGER) 
 @MapKey(name = "attributeName") 
 public Map<String, SentimentFrequencyCounts> getModelData() {
  return modelData;
 }

但这在运行时给了我以下异常org.hibernate.AnnotationException:使用@OneToMany 或@ManyToMany 定位未映射的类:

But this gives me the following exception at runtime org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class:

SentimentFrequencyCounts 类是我试图坚持的类的内部类.所以基本上我认为我真的不了解hibernate如何用于hashmap.真的很遗憾,我不能让它序列化并将其集中在一个列中.

The SentimentFrequencyCounts class is an inner class of the one I am trying to persist. So basically I think I really am not understanding how hibernate works for hashmap. Really a shame I can not just get it to serialize this and lump it up in a single column.

提前感谢您的帮助和时间.

Thanks in advance for your help and time.

推荐答案

去掉你现有的注解,用@Lob注解列表 - 这个指定一个持久属性或字段应该作为一个大对象持久化到一个数据库支持的大对象类型.

Take off your existing annotations and annotate the list with @Lob - this specifies that a persistent property or field should be persisted as a large object to a database-supported large object type.

如果变量的类型是 Serializable 的子类型,您可以完全省略注释;JPA 关于默认映射的规则规定,可序列化且非原始或 Embeddable 的类型被序列化并存储在 BLOB 列中.然而,List 是不可序列化的,尽管 ArrayList 是可序列化的.

If the type of the variable was a subtype of Serializable, you could just leave off the annotations altogether; JPA's rules on default mappings state that types which are Serializable and not primitive or Embeddable are serialized and stored in BLOB columns. However, List is not Serializable, even though ArrayList is.

您可以将@Lob 与@ElementCollection 一起使用,但我不确定结果是什么;我不知道这是否会序列化整个列表,或者创建一个表,其中每个列表元素都单独序列化.无论哪种方式,您都可能不感兴趣.

You can use @Lob with @ElementCollection, but i'm not sure what the result is; i don't know if that serializes the whole list, or creates a table in which each list element is serialized separately. It probably isn't of interest to you either way.

稍后当然,作为规范的勤奋的学生会知道,此注释仅适用于可序列化 type 的字段,而不是恰好包含可序列化 对象的字段>类.因此,要完成这项工作,您将不得不参与恶作剧.我看看你是否可以用一个以交叉类型为界的通用通配符做一些聪明的事情,但我认为你不能.但是,您可以编写一个像这样的小类:

MUCH LATER Of course, as a diligent student of the spec will know, this annotation only works for fields of Serializable type, not fields which merely happen to hold objects of a Serializable class. Consequently, to make this work, you will have to engage in shenanigans. I had a look at whether you could do something clever with a generic wildcard bounded with an intersection type, but i don't think you can. You can, however, write a little class like this:

class SerializableInstanceOf<T> implements Serializable {
    public final T instance;

    public SerializableInstanceOf(T instance) {
        Serializable s = (Serializable)instance;
        this.instance = instance;
    }
}

并将其用作列表的持有者 - 实体有一个用@Lob 标记的此类字段,并在其中保留对列表的引用.每次您想使用列表时,您都需要通过实例字段,可能是通过实体上的 getList 方法.

And use that as a holder for the list - the entity has a field of this type marked with @Lob, and keeps a reference to the list in it. Every time you want to work with the list, you go via the instance field, probably via a getList method on the entity.

它很丑,但它应该让你做你需要做的事情.

It's ugly, but it should let you do what you need to do.

这篇关于如何使用休眠持久化 HashMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆