2010-12-20 69 views
2

映射地圖集是否有JPA的方式映射給出如下類別與類型屬性Map<String, Set<Address>>如何在JPA

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

class Address { 
    int id; 
    String country; 
} 

有三個表:

tbl_company 
    id INT 

tbl_address 
    id INT 
    country VARCHAR(40) 

tbl_company_address 
    company_id INT 
    address_id INT 

如何映射此場景與JPA

回答

1

可能的解決方案之一是有一個地址s包裝類,並將Set插入該類。在這種情況下,您將能夠使用地圖(使用@OneToMany,@MapKey註釋)。例如。

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

..和AddressWrapper將包含@OneToMany Set<Address> addresses;,與String countryKey一起。