如何用Hibernate保持HashMap [英] How to persist a HashMap with hibernate

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

问题描述

你好,我对冬眠世界非常陌生,似乎遇到了障碍。我需要存储的对象中有一个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;

事情是我永远不需要用这张地图搜索,排序或做任何事情,我只需要将其与对象一起保存并在加载对象时加载它,所以我希望有一些方法可以让hibernate序列化它,然后将它存储在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.

感谢您的帮助和时间。

推荐答案

取消您现有的注释并使用@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列中。然而,即使ArrayList是List,List也不是可序列化的。

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.

你可以在@ElementCollection中使用@Lob,但我不确定结果是什么;我不知道该序列化整个列表,还是创建一个表,其中每个列表元素都是分别序列化的。它可能不是你感兴趣的任何方式。

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.

大量编辑:当然,作为规范的勤奋学生会知道,这个注释只适用于字段的Serializable 类型,而不是仅仅持有Seri​​alizable 的对象的字段。因此,要做到这一点,你将不得不参与诡计。我看看你是否可以用一个交叉类型来限制泛型通配符,但我认为你不能。然而,你可以写一个这样的小类:

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.

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

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