-->
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 by example / Criteria API with OneToMany relation
PostPosted: Thu Oct 06, 2011 7:11 am 
Newbie

Joined: Thu Oct 06, 2011 6:37 am
Posts: 1
Hi,

I have an entity "User", which has a OneToMany relationship to its communication data.

Code:
@Entity
public class User
{
    ....

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    public Set<CommunicationData> getCommunicationData() {
        return communicationData;
    }

    public void setCommunicationData(Set<CommunicationData> communicationData) {
        this.communicationData = communicationData;
    }
}


So, a user can have several communictation data, like phone, email, IM, ...
The CommunicationData entity has just a "type" and "data" property.

I want to search a user by example.

Code:
Criteria userCriteria = session.createCriteria(User.class);
Example userExample = Example.create(user);
userCriteria.add(userExample);
return userCriteria.list();


This works for a "simple" user example.

However, I also want to find a user by his email-address and/or phone number.

I added this:

Code:
        if (user.getCommunicationData() != null && user.getCommunicationData().size() > 0) {
            Criteria communicationCriteria = userCriteria.createCriteria("communicationData");         
            for (CommunicationData communicationData : user.getCommunicationData()) {
                Example communicationExample = Example.create(communicationData);
                communicationCriteria.add(communicationExample);
            }
        }


But it didn't work. I recognized, that the query does not contain any WHERE clause which compares the example.
I ended up adding

Code:
communicationCriteria.add(Restrictions.eq("data", communicationData.getData()));
communicationCriteria.add(Restrictions.eq("type", communicationData.getType()));


but it seems, this is just a bad workaround (since it is not really "by example" anymore). Happily, the entity only has 2 properties.

It works, as long I only add the email address, to the example. When I also want to search for phone, it doesn't work anymore, due to this SQL:

Code:
and communicat1_.data=?
        and communicat1_.type=?
        and (
            communicat1_.type=?
        )
        and communicat1_.data=?
        and communicat1_.type=?
        and (
            communicat1_.type=?
        )




Do you have any idea, how to better solve this problem? Thanks.


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.