Reputation: 1664
I have a contact entity that has >1 phone number contact[contact, name, cell,work,home...]
and want to create a lookup table phone_number[uid,contactuid,telephonenumber]
so that I can search through this table by telephone number to find the contact.
With JPA - how would I configure to;
phone_number
entity so that the entity will populated from the contact entity phone_number
record when the contacts number remove (or the contact is removed, remove all records)phone_number
record when the contacts number changes?I was hoping to do all this in the DAO as this isn't really domain logic..
**Update - is the contact->phone_number
relationship sensible to define in JPA or just map it using SQL in the DAO?
Many Thanks in advance
Upvotes: 0
Views: 473
Reputation: 691635
If I understand correctly, you already have a Contact entity with several phone number fields:
And you would like to find a contact given a phone number. If so, you don't need any additional table to do that:
select c from Contact c
where c.cellPhone = :phoneNumber
or c.workPhone = :phoneNumber
or c.homePhone = :phoneNumber
But maybe you should have a Phone entity with a phoneNumber
and a type
(work, cell, etc.) fields, and have a bidirectional OneToMany association between Contact and Phone. Your query would then be
select c from Phone p
inner join p.contact c
where p.phoneNumber = :phoneNumber
which would certainly be more efficient.
Upvotes: 1