开发者

How to map a Map of Sets in JPA

Is there a way in JPA to map an attribute with type Map<String, Set<Ad开发者_Python百科dress>>

Given the following classes:

class Company {
  int id;
  Map<String, Set<Address>> addresses; // Key is the country of the Address
}

class Address {
  int id;
  String country;
}

There are three tables:

tbl_company
  id INT

tbl_address
  id INT
  country VARCHAR(40)

tbl_company_address
  company_id INT
  address_id INT

How can I map this scenario with JPA


One of possible solutions is having an address-wrapping class, and insert Set into that class. In that case you will be able to use map (using @OneToMany, @MapKey annotations). E.g.

@OneToMany(mappedBy = ...)
@MapKey(name = "countryKey")
private Map<String, AddressWrapper> addressWrappers;

.. and AddressWrapper would contain @OneToMany Set<Address> addresses;, along with String countryKey.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜