COUNTRY和ADDRESS表具有一对多关系,ADDRESS和COUNTRY具有多对一关系。 COUNTRY表具有Id和国家/地区名称。地址表包含Country(具有COUNTRY表中的国家/地区代码)和NAME。 NAME和COUNTRY是ADDRESS表中的唯一键。
如何将Address Entity的NAME添加到CountryAddress Embeddable类? (可嵌入属性来自不同的实体)
@Entity
public class Country {
@Column(name = "COUNTRY")
private String countryName;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "country")
public Map<CountryAddress, Address> getAddressMap() {
return addressMap;
}
@Embeddable
public class CountryAddress {
@Column(name = "COUNTRY")
private String countryName;
// Need to add NAME from Address
}
}
@Entity
public class Address {
private Integer id;
@Column(name = "NAME")
private String name;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "COUNTRY")
private Country country;
}