Following an relation mapping example for Maps, I have
EMPLOYEE
ID FIRSTNAME LASTNAME SALARY
1 Bob Way 50000
2 Sarah Smith 60000
PHONE
ID OWNER_ID PHONE_TYPE AREACODE NUMBER
1 1 home 613 792-7777
2 1 cell 613 798-6666
3 2 home 416 792-9999
4 2 fax 416 798-5555
Example of a map key column relationship annotation
@Entity
public class Employee {
@Id
private long id;
...
@OneToMany(mappedBy="owner")
@MapKeyColumn(name="PHONE_TYPE")
private Map<String, Phone> phones;
...
}
@Entity
public class Phone {
@Id
private long id;
...
@ManyToOne
private Employee owner;
...
}
Is there any way to signal that OWNER_ID and PHONE_TYPE is the composite primary key?
There is no way to signal a composite primary key, because the PHONE_TYPE is part of the Map in the Employee
class and OWNER_id is part of the Phone
class. Beside that, that is also not your primary key: the primary key is the id property (as you have it). What you actually need is to specify a unique key over those two columns. In order to do that, one solution would be to give up the Map
in the Employee class and to move the phoneType
in the Phone
class, where you can then add the UniqueConstraint
.
PS: Of course, if you can/want you can simply the constraint on the DB-level, without any changes on the level of the Java code.