如何将复合唯一键(address1和address2)作为Map键。像@MapKey(name =" address1",name =" address2")而不是单个唯一键@MapKey(name =" address1")。
@Entity
public class Person {
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "person")
@MapKey(name = "address1")
public Map<String, Address> getAddressMap() {
return addressMap;
}
}
@Entity
public class Address {
private Integer id;
private String address1;
private String address2;
private Person person;
}
答案 0 :(得分:0)
使用Embeddable类型。
创建一个可嵌入的类(即PersonAddress
)来封装您的address1
和address2
属性。然后使用该embeddable类作为Map键。
@Entity
public class Person {
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "person")
public Map<PersonAddress, Address> getAddressMap() {
return addressMap;
}
}
@Entity
public class Address {
private Integer id;
@Embedded
private PersonAddress;
@ManyToOne
private Person person;
}
@Embeddable
public class PersonAddress {
private String address1;
private String address2;
}