-->
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 API when wanting to match a value in a String set?
PostPosted: Fri May 06, 2005 4:34 pm 
Beginner
Beginner

Joined: Thu Dec 09, 2004 3:19 pm
Posts: 34
Hibernate 2.1

I'm trying to create a Criteria search where one of the class members is a set. The snippet of hbm.
<class name="Foo">
<set name="bars" cascade="all">
<key column="FOO_ID" />
<element type="string" column="barName" />
</set>
</class>

How do I create a Criteria search when I want all Foo that have a value "specificBarName" in the "bars" set?

getSession().createCriteria( Foo.class )
.createCriteria( "bars" )
.add( ??? )

I can't find an Expression that works here. All expressions seem to expect to navigate an object. Here I'm basically saying "All Foo where foo.bars contains 'specificBarName'".

Is this possible with the Criteria API? All the examples seem to assume I have a set of some other full Java class and I'm looking at a property of that class (ala the Bid and User example in Hibernate in Action).


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 06, 2005 4:43 pm 
Regular
Regular

Joined: Mon Jul 26, 2004 2:28 pm
Posts: 86
Location: Pensacola, Florida
If the query you are after is something along the lines of this:

Code:
select foo.*
from foo f inner join bar b on f.id=b.fooId
where b.property='value'


Then the following should work:

Code:
session.createCriteria( Foo.class )
   .createAlias( "bars", "b" )
   .add( Expression.eq( "b.property", value )
   .list( );


Which is, in simple cases, equivalent to this:

Code:
select foo.*
from foo
where foo.id in (
   select fooId
   from bar
   where property='value')


Hope that helps,

- Jesse


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 06, 2005 6:08 pm 
Beginner
Beginner

Joined: Thu Dec 09, 2004 3:19 pm
Posts: 34
Unfortunatley, I must not be grasping it.

I end up getting a "java.sql.SQLException: ORA-00936: missing expression" when it tries to create the SQL. The SQL looks like:

select this.ID as ID0_, this.copy as copy0_, from MYSCHEMA.Foo this, MYSCHEMA.barNames x0_ where this.copy like ? and this.ID=x0_.FOO_ID

Not sure what's wrong with it. I'm definiately not an SQL expert on anything complex. I don't see anything in that SQL that deals with the barName I'm looking for either.


Quote:
add( Expression.eq( "b.property", value )


Where does the "property" bit come from? Is that documented somewhere as a way to query against sets of primitive types? Again, "b" is just a <set> of type string. It's not an object with any kind of accessor.[/url]


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 06, 2005 6:25 pm 
Beginner
Beginner

Joined: Thu Dec 09, 2004 3:19 pm
Posts: 34
Oh, and right before the SQL it has:
net.sf.hibernate.MappingException: collection was not an association: Foo.barNames.

This only shows up though when I try the query mentioned. If I do a simple query just on Foo.id, there's no problem. So the MappingException is presumably related to the issue.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 10, 2005 9:53 am 
Regular
Regular

Joined: Mon Jul 26, 2004 2:28 pm
Posts: 86
Location: Pensacola, Florida
Oh, whoops, wasn't paying attention. I'm sure there is an elegant way to do that using Criteria queries or HQL, but all I can think of right now is something like this:

Code:
session.createCriteria( Foo.class )
   .add( Expression.sql( "exists (select * from bar where foo_id={alias}.foo_id and barName=?)", barName, Hibernate.STRING )
   .list( );


It seems like a shortcut, and will only work in databases that support correlated subqueries in the WHERE clause. Maybe there is something in Hibernate 3.0 (I haven't yet taken the plunge).

- Jesse


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 10, 2005 1:55 pm 
Beginner
Beginner

Joined: Thu Dec 09, 2004 3:19 pm
Posts: 34
Thanks. Yea, I've given up on using the Criteria interface. I found another post where Gavin said the Criteria interface doesn't support it.

I'm still having problems getting HQL to do what I want though :(. I can write the HQL easily enough to do the join, it's the WHERE clause that's an issue.

select f
from Foo f
join f.bars b
where b.??? like "zippy"

Where Foo is basically:
public class Foo
{
/** A set of String */
Set bars = new LinkedHashSet();
}

Since "bars" isn't a Set of mapped class, I'm not sure what the where clause is supposed to look like. I'm just doing trial and error on the syntax...


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.