-->
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.  [ 3 posts ] 
Author Message
 Post subject: One-to-many search with Criteria
PostPosted: Fri Jul 09, 2010 10:56 am 
Newbie

Joined: Fri Apr 16, 2010 11:29 am
Posts: 5
Hi all,

I've got a Hibernate entity, called Event, which has a one-to-many metadata entity, EventData.

Given the following Event:

EventId: 1
EventHash: broccoli

With the following EventDatas:

EventDataId: 1
EventId:1
Field: tag
Value: tagme

EventDataId: 2
EventId: 1
Field: tag
Value: anotherTag

How do I create a Criteria query to retrieve the event which has BOTH tags "anotherTag" and "tagme"? In SQL, I'd join the event_data table once for each tag being searched for, but I can only seem to create one alias for the Event.EventData relationship, i.e.

Code:
   int inc = 0;

   Conjunction junc = Restrictions.conjunction();

   for ( String tag : tags ) {
      crit.createAlias("e.EventData", "ed"+inc);
      junc.add(
         Restrictions.and(
            Restrictions.eq("ed"+inc+".field", "tag"),
            Restrictions.eq("ed"+inc+".content", tag)
         )
      );
      inc++;
   }


Doesn't work; "duplicate association path: Event.EventData"

Similarly, a normal Conjunction doesn't work, because the clause ends up as:

Code:
((ed3_.field='tag' and ed3_.content='anotherTag') and (ed3_.field='tag' and ed3_.content='tagme'))


and, obviously, a database field can't have two different values at the same time.

Any ideas as to how I could clean this up, or is the only option reverting to HQL?


Top
 Profile  
 
 Post subject: Re: One-to-many search with Criteria
PostPosted: Sun Jul 11, 2010 5:12 am 
Newbie

Joined: Fri Jul 09, 2010 10:26 am
Posts: 2
Hi

why u want to go for criteria , IF EventId is mapped with child table EventDatas (one-to-many) you can directly get it using event.getEventDatas() for respective Id.

Regards
Arun


Top
 Profile  
 
 Post subject: Re: One-to-many search with Criteria
PostPosted: Sun Jul 11, 2010 1:07 pm 
Newbie

Joined: Fri Apr 16, 2010 11:29 am
Posts: 5
Hi Arun,

The point is I'm *searching* for these Events, using EventData.Content values as the criteria for the search. I basically want to find all EventDatas where field == tag and content == "anotherTag", but without having to do two queries - one to find the eventIds, and another to load the actual Events.


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

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.