映射一个java.util.Map&LT;实体,实体GT;在使用Hibernate JPA注解 [英] Mapping a java.util.map<entity, entity> in hibernate using JPA annotations
问题描述
我似乎,在Hibernate中,其中的关键和资源都是实体由Hibernate映射映射集合类的问题。
I seem to be having an issue with mapping a collection in hibernate where the key and the resource are both entities to be mapped by hibernate.
而从文档(我对这个问题的主要资源一直是这个,但我当然欢迎任何人:的 http://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/collections.html )它会出现这应该是一个相对简单的任务,我似乎无法获得地图的关键要坚持。
Whereas from the documentation (my main resource for this issue has been this, but I of course welcome any others: http://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/collections.html) it would appear that this should be a relatively simple task, I just can't seem to get the key of the map to persist.
,因为它代表我的code是这样的:
My code as it stands looks like this:
父类:
@Entity
public class Parent {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@MapKey
@MapKeyClass(Key.class)
@ManyToMany(cascade = CascadeType.ALL)
private Map<Key, Resource> map;
Key类:
@Table(name="ParentKey")
@Entity
public class Key implements Comparable<Key> {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "hierarchyKey")
private int key;
的资源类是一个相对简单的POJO并持续正常,所以我认为这个问题是不是在这个类。
The resource class is a relatively simple POJO and persists fine, so I would assume that the issue is not in that class.
我也碰到过对Hibernate的论坛上的一些文件中的注释@MapKeyManyToMany,但我相信这之后一直去precated。
I did come across the annotation @MapKeyManyToMany in some documentation on the hibernate forums, but I believe this has since been deprecated.
该数据库的架构是不重要的在这个阶段,主要关注的只是确保一切持续到数据库中。我们正在使用MySQL的底层数据库。
The schema of the database is unimportant at this stage, the main concern is simply making sure everything persists to the database. We're using MySQL for the underlying database.
正如我敢肯定,你可能已经猜到,任何涉及词地图搜索,休眠,实体按键返回大量不相关的这个问题的问题,但我敢肯定,这必须是发生相当的情况常在映射的情况而言。
As I'm sure you can probably guess, any search involving the words map, hibernate, entity and key return a large amount of questions not related to this issue, though I'm sure this must be a situation that occurs quite often in terms of mapping situations.
我不经常提出的问题,但我是一个积极的读者,所以请让我知道,如果需要任何更多的信息。
I don't often submit questions, but I am an active reader, so please let me know if any more info is required.
推荐答案
按规定这工作。操作不级联到映射的键。它们级联只的值。在文档此被告知用下面的话:
This works as specified. Operations are not cascaded to the key of the map. They are cascaded only to the value. In documentation this is told with following words:
在目标集合是一个java.util.Map,级联元素
适用于地图值
When the target collection is a java.util.Map, the cascade element applies to the map value.
重点必须单独保留。
这篇关于映射一个java.util.Map&LT;实体,实体GT;在使用Hibernate JPA注解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!