Mannie
Mannie

Reputation: 1664

JPA - How do I map to a quick-lookup table?

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;

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

Answers (1)

JB Nizet
JB Nizet

Reputation: 691635

If I understand correctly, you already have a Contact entity with several phone number fields:

  • cellPhone
  • workPhone
  • homePhone

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

Related Questions