Hi,
yes that about a constant value is often a requirement;
I usually have an enum type for the Address
(homeAddress, workAddress, ....) or you may prefer an Integer, so you can later add more types to your DB without changing the code.
The solution is to use a Map binding:
Code:
@Entity
public class Person {
@Id
private Long id;
@OneToMany(mappedBy="ofPerson")
@MapKey(name="addressType")
private Map<AddressType,Address> addresses;
...
}
@Entity @IdClass(AddressPk.class)
public class Address {
@Id @Enumerated
@Column(name = "address_type_id", nullable = false, updatable = false )
private AddressType addressType;
@Id @ManyToOne
@JoinColumn(name = "person_id", nullable = false, updatable = false )
private Person ofPerson
...
}
You should add an appropriate AddressPk and use the same column names; if you get it right you get a very nice double key in Address: PK (type,person_id FK)