Hi,
I have 2 entities: Person and Phone. Person has a phoneNumber field for a main phone number and One-To-Many relationship with the Phone entity, that is a collection of additional phone numbers for this person.
Code:
@Entity
public class Person {
private String phone;
@OneToMany(mappedBy = "phoneOwner", cascade = CascadeType.ALL)
private List<Phone> additionalPhones;
...
}
Code:
@Entity
public class Phone {
private String number;
@ManyToOne(optional = false)
private Person phoneOwner;
...
}
I have to write a query that searches all People by phone number that is either a phone in the Person entity, or one of the additional phones for this person. My query is:
select p from Person p, in (p.additionalPhones) ap where p.phone like '%" + phone + "%' or ap.number like '%" + phone + "%'
and it is working only if the person has additional phones which are not required (most of them don't have). If there are not additional phones, the person is not in the result even if there is a match with the main phone number. Is it a normal behaviour for
in (some collection) and how can I write it so it searches even if there are no additional phones?