如何映射一个嵌套的集合,地图<关键,列表与LT;价值观与GT;>中使用Hibernate JPA注解? [英] How do I map a nested collection, Map<Key,List<Values>>, with hibernate JPA annotations?

查看:200
本文介绍了如何映射一个嵌套的集合,地图<关键,列表与LT;价值观与GT;>中使用Hibernate JPA注解?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,我不知道如何正确地标注。

我对持有人::数据目标:


  • 列表不应该被比较,而是由数组中的元素的自然顺序维持秩序。 (可以是一个NDX列,如果这是有帮助的。)

  • 持有人将有数据仅供参考,所以所有级联可能是适用为好。

我也开出了不同的设计,消除了地图,如果这将使一个更清洁的设计。

  @Entity
公共类持有人扩展的domainObject {
  私人地图<枚举,列表与LT;组件>>数据;
}@实体
公共类元素扩展domainObject的{
  私人长期VALUEID;
  私人诠释otherData;
}@MappedSuperclass
公共类domainObject的{
 //提供ID
 //乐观锁定
 //创建和更新日期
}


解决方案

我不认为这是可能的休眠(-core)映射集合的任何集合:


  

集合几乎可以包含任何
  其他的Hibernate类型,包括所有
  基本类型,自定义类型,组件,
  当然,引用其他
  实体。


(从<一个href=\"http://docs.jboss.org/hibernate/stable/core/reference/en/html/collections-mapping.html#collections-elements\">the官方文档)

注意在几乎并集合类型的遗漏。

一个解决办法:你需要'之间'的收集架和元素引入一个新的类型。这种类型可以映射为一个实体或元件,它是指在地图的原始内容,在这种情况下,列表。

是这样的:

  @Entity
公共类持有人扩展的domainObject {
  @OneToMany
  私人地图&LT;枚举,插图中&GT; inBetweens;
}@实体
公共类插图中扩展的domainObject {
  @OneToMany
  私人列表&LT;组件&gt;要素;
}@实体
公共类元素扩展domainObject的{
  私人长期VALUEID;
  私人诠释otherData;
}@MappedSuperclass
公共类domainObject的{
 //提供ID
 //乐观锁定
 //创建和更新日期
}

映射的其余部分取决于特定的情况,但相当简单。

I have a class I am not sure how to annotate properly.

My goal for Holder::data:

  • List should maintain order not by comparator but by the natural ordering of the elements in the array. (Which can be an ndx column if that is helpful.)
  • Holder will have the only reference to data, so Cascade all is probably applicable as well.

I am also open to a different design that removes the map, if that would make for a cleaner design.

@Entity
public class Holder extends DomainObject {
  private Map<Enum,List<Element>> data;
}

@Entity
public class Element extends DomainObject {
  private long valueId;
  private int otherData;
}

@Mappedsuperclass
public class DomainObject {
 // provides id
 // optimistic locking
 // create and update date
}

解决方案

I don't think it is possible with hibernate(-core) to map any collection of collections:

Collections may contain almost any other Hibernate type, including all basic types, custom types, components, and of course, references to other entities.

(from the official doc)

Notice the almost and the omission of the collection type.

A workaround: You need to introduce a new type 'in between' the collection holder and the element. This type you can map as an entity or a component and it refers the original content of the map, in this case a list.

Something like:

@Entity
public class Holder extends DomainObject {
  @OneToMany
  private Map<Enum,InBetween> inBetweens;
}

@Entity
public class InBetween extends DomainObject {
  @OneToMany
  private List<Element> elements;
}

@Entity
public class Element extends DomainObject {
  private long valueId;
  private int otherData;
}

@Mappedsuperclass
public class DomainObject {
 // provides id
 // optimistic locking
 // create and update date
}

The rest of the mapping depends on your particular situation, but is rather straightforward.

这篇关于如何映射一个嵌套的集合,地图&LT;关键,列表与LT;价值观与GT;&gt;中使用Hibernate JPA注解?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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