简体   繁体   中英

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.

Thanks in advance, Neuquino

Try the hibernate annotation MapKey

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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