简体   繁体   English

如何坚持地图 <String, List<Object> &gt;在Hibernate中

[英]How to persist a Map<String, List<Object>> in Hibernate

I've got a Map containing MyObject instances. 我有一个包含MyObject实例的Map The MyObject class uses JPA to persist its fields: MyObject类使用JPA来保存其字段:

@OneToMany(cascade = CascadeType.ALL)
@LazyCollection(LazyCollectionOption.FALSE)
private Map<String, MyObject> results = new HashMap<String, MyObject>();

We changed the value stored by the Map to a List : 我们将Map 存储更改为List

private Map<String, List<MyObject>> results = new HashMap<String, List<MyObject>>();

But upon launching we receive a stack trace: 但是在启动时我们会收到一个堆栈跟踪:

Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.me.myapp.MyObject.results[java.util.List]
    at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1150)
    at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:680)
    at org.hibernate.cfg.annotations.MapBinder$1.secondPass(MapBinder.java:107)
    at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:66)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1221)
    at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:383)
    at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1206)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:673)
    at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1368)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1334)
    ... 30 more

Does Hibernate not support persisting a Map containing (as values) List types? Hibernate不支持持久化包含(作为值) List类型的Map吗? Or are my annotations incorrect? 或者我的注释是不正确的? I haven't found this particular configuration in any of the documentation or examples. 我没有在任何文档或示例中找到此特定配置。

Personally, I don't think this is a good use of Hibernate. 就个人而言,我不认为这是一个很好用的Hibernate。 There's no abstraction here. 这里没有抽象。 It would make sense if you had a model object with a one-to-many relation expressed as a child List as a data member in the parent. 如果您将具有一对多关系的模型对象表示为子List作为父级中的数据成员,那将是有意义的。

My advice? 我的建议? Don't use Hibernate. 不要使用Hibernate。 Use straight JDBC, Spring's JDBC template, or something like iBatis. 使用直接JDBC,Spring的JDBC模板或类似iBatis的东西。

ORM stands for "Object Relational Mapping". ORM代表“对象关系映射”。 You have tables, so you've got the relational part. 你有桌子,所以你有关系部分。 You've got data that you can assign to columns in tables. 您有可以分配给表中列的数据。

But it sounds to me like you've got no Objects. 但听起来像你没有物体。 So why use Hibernate? 那么为什么要使用Hibernate呢?

The objects stored in the map must be the target object of the oneToMany association (always mapped). 存储在地图中的对象必须是oneToMany关联的目标对象(始终映射)。 You can't store arbitrary objects or collections there. 您不能在那里存储任意对象或集合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM