-->
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: Feature request criteria
PostPosted: Tue Jul 19, 2005 4:47 am 
Regular
Regular

Joined: Tue Jan 27, 2004 12:22 pm
Posts: 103
Hibernate version:3.0

It would be functional to have a function like criteria.getCriteria("foo"),
which:
1. returns a existing criteria object associated with the foo
2. creates the criteria if it doesn't exist.

Usage scenario:

class myAction

Criteria foo = ..FooBarClass.class..
// Set some default selection criteria

// add user selection criteria
// Error: FooBarClass.associatedClass already exists in criteria

_________________
Dencel
- The sun has never seen a shadow -


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 19, 2005 5:46 am 
Regular
Regular

Joined: Fri Sep 17, 2004 10:51 am
Posts: 61
Location: Rimini, Italy
That's what the named query are there for.
Why a criteria would be more useful for you ? And a kinda complex cache must be used to handle Criteria, IMO is much more the pain than the gain. Can you provide a better use case for what you think ?

_________________
--
Marco


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 19, 2005 6:13 am 
Regular
Regular

Joined: Tue Jan 27, 2004 12:22 pm
Posts: 103
Hi marco,

That's what I'm doing now.. keeping a simple Hashtable cache for the different criteria objects (that's not complex).

The use case is following:
I want to seperate the Ordering and Selection criteria in my source code. Thus I require to re-use the different Criteria objects (I have simple logic to retrieve the criteria object based on property provided).
I don't know the implementation of Criteria, but isn't there a reference to the previous Criteria objects? This would make the get method simple to implement.

If naming aliases is the only solution I will implement it that way, but I would prefer this way.

_________________
Dencel
- The sun has never seen a shadow -


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 19, 2005 6:29 am 
Regular
Regular

Joined: Fri Sep 17, 2004 10:51 am
Posts: 61
Location: Rimini, Italy
For what I know, every method in Criteria modifies the objects and return a reference to the object itself (return this).
To implement what you're saying, you should clone the Criteria object and then apply the order, but I don't know if it's possible.
In a similar situation, I didn't bother to cache the Criteria object (is it really a big deal ?) and simply re-applied the method (in my case was the projection, but it's the same with order ).

_________________
--
Marco


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 19, 2005 7:11 am 
Regular
Regular

Joined: Tue Jan 27, 2004 12:22 pm
Posts: 103
I don't really understand what you mean with reapply. Can you give an example? Currently I am doing something like this:

Code:
Criteria c = session.createCriteria(FooClass.class)
.add(Restri...);
localCriteriaCache.put("root",c);
c=c.createCriteria("bar").add(Restr...);
localCriteriaCache.put("bar",c);


and later in the code...
Code:
localCriteriaCache.get("bar").addOrder(Order.desc("foobar");


Note: this code gives an error
Code:
Criteria c = session.createCriteria(FooClass.class)
.add(Restri...)
.createCriteria("bar").add(Restr...);
....
c.createCriteria("bar").addOrder(Order.desc("foobar"));

_________________
Dencel
- The sun has never seen a shadow -


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 19, 2005 9:31 am 
Regular
Regular

Joined: Fri Sep 17, 2004 10:51 am
Posts: 61
Location: Rimini, Italy
Dencel wrote:
Note: this code gives an error
Code:
Criteria c = session.createCriteria(FooClass.class)
.add(Restri...)
.createCriteria("bar").add(Restr...);
....
c.createCriteria("bar").addOrder(Order.desc("foobar"));


I may be wrong, but the error is cause by the createCriteria("bar"), 'cause it declares a self join without specifying an alias. Try to just add the order:
Code:
Criteria c = session.createCriteria(FooClass.class)
.add(Restri...)
.createCriteria("bar").add(Restr...);
....
c.addOrder(Order.desc("foobar"));

The problem is that the method addOrder just adds the order, so if you apply a different order later on the same criteria, the orders will be applied together... uhm, so the only solution seems to be to clone the criteria object... [/b]

_________________
--
Marco


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 19, 2005 12:06 pm 
Regular
Regular

Joined: Tue Jan 27, 2004 12:22 pm
Posts: 103
Marco,

Your solution doesn't solve my problem. Anyway I'll check the sourcecode and see how it's implemented. Rewarding you the credits for the help. Thank you.

_________________
Dencel
- The sun has never seen a shadow -


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.