-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Query to a collection that is not required field for entity
PostPosted: Wed Oct 13, 2010 9:29 am 
Newbie

Joined: Wed Oct 13, 2010 8:48 am
Posts: 2
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?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.