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.  [ 7 posts ] 
Author Message
 Post subject: Nested Criterias and Junction conjunction and disjunction.
PostPosted: Thu May 27, 2004 10:58 am 
Regular
Regular

Joined: Tue Dec 09, 2003 2:39 pm
Posts: 106
Location: Toronto, Canada
The Criteria and Junction API seems to be a nice fit for a filtering use case we have that also requires logical operations.

In our DAO when building the Criteria object we determine what type of junction the user wishes to apply in this way:

Code:
        ...
        //add the and or operation
        if (staticFilter.isAndOperation()) {
            junction = Expression.conjunction();
        }
        else {
            junction = Expression.disjunction();
        }
        ...


Generally, we add our criterion passed by our client in this way:

Code:
        ...
        junction.add(Expression
                        .like("title", title, MatchMode.ANYWHERE).ignoreCase());
        ...


Later, we add the junction to a Criteria object.

Code:

         ...
         Criteria criteria = session.createCriteria(SomeEntity.class).add(junction);
         ...


Further down, we also have some additional criteria we add to the recently created criteria object:

Code:
           ...
           criteria = criteria.createCriteria("metapropertiesValues");
           criteria.add(Expression.eq("value",  metapropertyValue));
           criteria.add(Expression.eq("id.problemMetaproperty.id", 
           metapropertyKey));
           ...


Question:

We'd like the criteria's junction to apply to the additional criteria we specified above. Currently the criteria object would treat this as an "AND" type criteria only and of course disregards the previously added junction.

We're fairly new to the Criteria API. Is this possible? Can we have nested criteria that uses a single junction? We will have several of these nested criterias that we want to include in addition to the one above.

Or are we missing something?

Regards,
Roll


Top
 Profile  
 
 Post subject: HQL
PostPosted: Thu May 27, 2004 8:42 pm 
Regular
Regular

Joined: Tue Dec 09, 2003 2:39 pm
Posts: 106
Location: Toronto, Canada
I should note that part of the reason for asking is because we are trying to determine if we should abandon our usage of the Criteria API in favor of the more mature HQL which we are comfortable with.

Any guidance appreciated.


Top
 Profile  
 
 Post subject: Should I rephrase this question?
PostPosted: Sun May 30, 2004 2:36 pm 
Regular
Regular

Joined: Tue Dec 09, 2003 2:39 pm
Posts: 106
Location: Toronto, Canada
I can provide more details if necesary.

Regards,
Roll


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 30, 2004 3:20 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Code:
s.createCriteria(Foo.clas).createAlias("bars", "bar").add(
    Expression.disjuction()
         .add( Expression.eq("bar.id", barId) )
         .add( Expression.eq("name", fooName) )
).list();


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 09, 2004 7:57 pm 
Newbie

Joined: Fri Jun 25, 2004 4:25 pm
Posts: 4
I have a question regarding this use of disjunction. In this example:
Code:
s.createCriteria(Foo.clas).createAlias("bars", "bar").add(
    Expression.disjuction()
         .add( Expression.eq("bar.id", barId) )
         .add( Expression.eq("name", fooName) )
).list();

Does this return two instances if something matches both of the expressions? It seems to do this in my application. If this is the correct behavior, is there an easy way to get unique results from criteria.list()? Thanks for the help!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 28, 2005 7:51 am 
You probably need to tell Hibernate to collate the child collections out of the query properly:

baseCriteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

This is surprisingly not default behaviour.


Top
  
 
 Post subject:
PostPosted: Sun Jan 22, 2006 9:16 am 
Beginner
Beginner

Joined: Sun Jan 22, 2006 6:56 am
Posts: 29
gavin wrote:
Code:
s.createCriteria(Foo.clas).createAlias("bars", "bar").add(
    Expression.disjuction()
         .add( Expression.eq("bar.id", barId) )
         .add( Expression.eq("name", fooName) )
).list();


How does this solve rollatwork's problem above? The desired behavior was to add a subquery to a disjunction...

I suppose this can be done by rewriting it as an EXISTS clause:


Code:
junction.add(Expression
                        .like("title", title, MatchMode.ANYWHERE).ignoreCase());

DetachedCriteria additionalCriteria = DetachedCriteria.forClass(Value.class);
additionalCriteria.add(Expression.eq("value",  metapropertyValue));
additionalCriteria..add(Expression.eq("id.problemMetaproperty.id",
           metapropertyKey));

junction.add(Subqueries.exists(additionalCriteria))


But still need to connect the detached criteria to the external query's alias somehow...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.