-->
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: session.createFilter
PostPosted: Wed May 10, 2006 1:23 am 
Newbie

Joined: Mon Sep 26, 2005 4:08 am
Posts: 15
Hi everyone,

i am trying to use filter collections in my code but there is a problem, i am not able to understand what is happening in the code below which is from Hibernate reference documentation. It is not expained anywhere how to use collection filters.

what is this pk.getKittens(), is it returning all the kittens and what is Color.BLACK and Hibernate.custom(ColorUserType.class)

there is a question mark (?) in the where clause, is a query specified somewhere?

Collection blackKittens = session.createFilter(
pk.getKittens(),
"where this.color = ?")
.setParameter( Color.BLACK, Hibernate.custom(ColorUserType.class) )
.list()
);

Please help.
thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 10, 2006 2:03 am 
Regular
Regular

Joined: Wed Aug 25, 2004 6:23 am
Posts: 91
Yes - createFilter actually creates a Query object. Maybe the code makes more sense like this:-

Code:
Query kittenColorFilterQuery = session.createFilter(pk.getKittens(), "where this.color = ?");
kittenColorFilterQuery.setParameter( Color.BLACK, Hibernate.custom(ColorUserType.class));
Collection blackKittens = kittenColorFilterQuery.list();


pk.getKittens() is the original mapped collection.
Color.BLACK is the value that we're setting for the parameter in the query and Hibernate.custom(ColorUserType.class) is its type.
"where this.color = ?" is the entire query supplied by the user - Hibernate introduces its own from clause to match to collection and aliases the elements to 'this'.

Hope that helps,
Cheers,
Rich.


Top
 Profile  
 
 Post subject: unrefereced collection
PostPosted: Wed May 10, 2006 2:09 am 
Newbie

Joined: Mon Sep 26, 2005 4:08 am
Posts: 15
Hi Rich,

I am trying to use collection filters but i am getting this error:
The collection was unreferenced

Here is what i am doing:

-- I have a xml mapping file which has a query defined:

<query name="tmp" cacheable="true">
<![CDATA[
SELECT M1 as Milestone
FROM Milestone M1
]]>
</query>

Next, i have a method which returns a list i.e.

public List tmp() {

Query query= getSession().getNamedQuery("tmp");

Query filterQuery = getSession().createFilter( getSession().getNamedQuery("tmp"), "");

return filterQuery.list();
}

now whenever i call this method, i get an error: The collection was unreferenced in line which says: return filterQuery.list();


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.