EclipseLink MapKey复合唯一键

时间:2017-10-06 15:35:12

标签: jpa eclipselink

如何将复合唯一键(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;

}

1 个答案:

答案 0 :(得分:0)

使用Embeddable类型。

创建一个可嵌入的类(即PersonAddress)来封装您的address1address2属性。然后使用该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;
}