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

那年仲夏 提交于 2019-12-11 08:30:00

问题


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.

Thanks in advance, Neuquino


回答1:


Try the hibernate annotation MapKey

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



回答2:


Google Collections has a class with this facility. Try it.



来源:https://stackoverflow.com/questions/4695976/how-to-map-onetomany-to-a-java-util-map-with-custom-keys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!