Hibernate(JPA)映射HashMap [英] Hibernate(JPA) mapping a HashMap

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

问题描述

预读:

如何使用hibernate持久化HashMap

我的问题反映了以下要使用JPA映射的结构:

My problem revolves aroung the following structure that I want to map with JPA:

Map<User, List<POJO>>

我的POJO非常简单(没有复合类型等,只是一些原语)。

My POJO is very simple (no composite types etc., just some primitives).

如何在链接的问题中实现建议?如何仅使用 @Lob 注释 List 部分(当我只是注释字段时,我得到一个类转换错误,因为HashMap不能被转换为Blob,这是我的问题的根源 - 我不能仅注释地图的部分值)?

How can I implement the advice in the linked question? How can I annotate only the List part with @Lob (When I just annotate the field I get a class cast error, because HashMap can't be cast to Blob, which is the root of my problem - that I can't annotate only the values part of the map)?

I '我不知道我是否需要一个实现Serializable包装List的包装器类型,或者仅仅使用ArrayList,这本身就是Serializable。
在任何情况下,我都不会坚持这个地图实例...

I'm not sure whether I need to make a wrapper type that implements Serializable that wraps the List, or it's enough to just use ArrayList, which is itself Serializable. And in any case, I'm not managing to persist this Map instance...

顺便说一句,这一切各不相同:我可以把列表作为每个用户的类成员,尽管我不觉得它属于那里,因为它不是用户数据(如帐户数据,名称,地址等)。它类似于购买,所以我把它们放在一个类似于实用程序的类(用户类的外部),它负责这些购买,以便拥有更多的模块化模型。我想听听关于这听起来是否合理的建议。

By the way, I'm open to advice about going about this all differently: I could just stick the List as a class member for each User, although I don't feel that it belongs there since it's not user data (like account data; name, address etc.). Its analogous to purchases, so I've placed them in a utility-like class (external to the User class) that takes care of these purchases, in order to have more modular models. I would like to hear advice as to whether or not this sounds sensible.

任何有用的建议将被奖励与虚构的曲奇饼(和upvote,显然)。

他们在非虚构的意义上是无脂的。

Any helpful advice will be rewarded with imaginary cookies (and upvotes, obviously).
They're fat free in a very non-imaginary sense.

干杯。

推荐答案

如果你有一张地图,你可能有一对多的关系。将您的列表保存在用户中,并使您的POJO成为一个实体,并且不要愚弄@Lob,该地图是等待发生的灾难(哈希值/等于问题),并且不会提供干净的数据库映射。

If you have a Map, you probably have a one-to-many relationship. Stick your list in User and make your POJO an entity, and do not fool around with @Lob, the Map is a disaster waiting to happen (hash value / equals issues) and does not give a clean database mapping.

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

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