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

class A{

  private List<B> bs;

  ...
}

class B{

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

And I would 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’s clear what I would like to do, but it is as simple as matching the one-to-many relationship with the card with the name B as the card key.

Thanks in advance, Neuquino

+1
source share
2 answers

Try hibernate MapKey annotation

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

Google Collections has a class with this tool. Give it a try.

0
source

Source: https://habr.com/ru/post/1785730/


All Articles