简体   繁体   English

如何使用自定义键将@OneToMany映射到java.util.Map?

[英]How to map @OneToMany to a java.util.Map with custom keys?

class A{

  private List<B> bs;

  ...
}

class B{

  private Long id;
  private String name;
  ...
} 

And I'd like to have this: 我想要这个:

class A{

  // the map should have B.name as key
  private Map<String,B> bs;

  ...
}

class B{
  private Long id;
  private String name;
  private A a;
  ...
} 

I don't know if it is clear what I'd like to do, but it is as simple as mapping a one to many relationship to a Map with the name of B as the key of the map. 我不知道我是否清楚我想做什么,但它就像将一个到多个关系映射到一个名为B的Map作为地图的关键字一样简单。

Thanks in advance, Neuquino 在此先感谢Neuquino

Try the hibernate annotation MapKey 尝试使用hibernate注释MapKey

@MapKey(name = "name")
@OneToMany()
private Map<String,B> bs;

Google Collections has a class with this facility. Google Collections有一个这个设施的课程。 Try it. 试试吧。

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

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