-->
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.  [ 6 posts ] 
Author Message
 Post subject: Criteria Query: Restrictions and Collections
PostPosted: Tue Aug 29, 2006 8:26 am 
Beginner
Beginner

Joined: Tue Aug 29, 2006 8:08 am
Posts: 34
Does any one knows if there must be special consideration when doing Query By Criteria thru a property which is a collection. eg.:

Having a class like next

public class LabeledItem {
private Long id;
private Set<Label> labels;
............
}


correctly mapped; I launch a criteria query like

criteria = session.createCriteria(LabeledItem.class)
.add(Restrictions.eq("labels",theLabels));


being theLabels a Set<Label>. As i want to retrieve only the
LabeledItems with exactly the indicated labels.

My problem is I'm getting the next error when executing the query:

Caused by: org.postgresql.util.PSQLException: No value specified for parameter 1.
at org.postgresql.core.v3.SimpleParameterList.checkAllParametersSet(SimpleParameterList.java:102)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:166)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:388)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:329)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:239)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:139)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1669)
at org.hibernate.loader.Loader.doQuery(Loader.java:662)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.doList(Loader.java:2145)
... 9 more


please help and thanx in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 30, 2006 3:23 am 
Beginner
Beginner

Joined: Tue Aug 29, 2006 8:08 am
Posts: 34
... one thing important i should have remarked: the property labels which
i mean to filter thru, is an association to an another class of many-to-many.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 30, 2006 6:28 am 
Newbie

Joined: Fri Aug 25, 2006 10:18 am
Posts: 8
Location: England
I think you need to add restrictions based on properties of Label.

For eaxample, if Label is defined as:

Code:
public class Label
{
    private Long id;
    private String name;
}

And you want all Labels starting with an 'A', you would use:

Code:
critLabeledItem = session.createCriteria(LabeledItem.class);
critLabel =  critLabeledItem.createCriteria("labels");
critLabel.add(Restrictions.eq("name", new String("A%")));

Please rate this reply.


Top
 Profile  
 
 Post subject: Almost ......
PostPosted: Wed Aug 30, 2006 8:53 am 
Beginner
Beginner

Joined: Tue Aug 29, 2006 8:08 am
Posts: 34
yeah that's what i first tried.....

criteria = session createCriteria(LabeledItem.class)
.createCriteria("labels");
for (Label label : allLabels2match) {
criteria = criteria.add(Restriction.eq("name",label.getName());
}

but that doesn't work as it is equivalent to this SQL:

select id_item from labeleditems li , label l
where li.id_item = l.iditem
AND l.name = 'name1'
AND l.name = 'name2'
AND l.name = 'name3'

.................

AND l.name = 'namen'

and this produces naturally 0 results. That's why i tried directly the property
- labels - without going deep into it.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 30, 2006 9:00 am 
Newbie

Joined: Fri Aug 25, 2006 10:18 am
Posts: 8
Location: England
Rather than ANDs, you want ORs. To do this you need to use Disjunction.

Code:
criteria = session.createCriteria(LabeledItem.class).createCriteria("labels");
Disjunction disjunction = Restrictions.disjunction();
for (Label label : allLabels2match)
{
    disjunction.add(Restriction.eq("name",label.getName());
}
criteria.add(disjunction);

Please don't forget to rate this answer.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 31, 2006 3:25 am 
Beginner
Beginner

Joined: Tue Aug 29, 2006 8:08 am
Posts: 34
Thanx 4 your help but that's not what i want.
Let me explain:

I have for instance next LabeledItems

A -> labeled [fiction, humour]
B -> labeled [fiction]
C -> labeled [history, fiction]

If i seek objects labeled fiction and humour, i expect to get just A, but not A
and B and C as a Disjunction would do; am i wrong?

I guess it rather must have something to do with grouping by items as the Object Model is mapped to tables this way:

LABELED_ITEM (idItem, idLabel) and LABELS (idLabel, name)


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