-->
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.  [ 5 posts ] 
Author Message
 Post subject: Way to check if detached object matches Criteria?
PostPosted: Sat Jul 16, 2005 4:23 pm 
Beginner
Beginner

Joined: Thu Jun 23, 2005 4:11 pm
Posts: 24
I know this seems impossible but is there a way to check if an object matches a Criteria without hitting the database?

Something like boolean objectMatches = criteria.matches(someObj)


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 16, 2005 9:58 pm 
Senior
Senior

Joined: Tue Jun 21, 2005 10:18 am
Posts: 135
Location: South Carolina, USA
If and only if you know the object is in one of the caches. Otherwise, you're asking, as you said, the impossible.

Can you tell me the price of Google stock without looking, assuming you didn't look a few moments ago?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 16, 2005 10:27 pm 
Beginner
Beginner

Joined: Thu Jun 23, 2005 4:11 pm
Posts: 24
I wondered the same thing about if it was in the cache. But given the example:

Cat cat1 = session.save(new Cat("snowball",120));

Cat cat2 = session.createCriteria(Cat.class)
.add( Restrictions.like("name", "snow%") )
.add( Restrictions.gt( "weight", new Float(100) ) )
.uniqueResult();

Is it possible that cat2 will be equal to cat1 without hitting the database, if all field comparisons are done in the database (no id is available)?

Re: Can you tell me the price of Google stock without looking, assuming you didn't look a few moments ago?

If I always know the price of google stock before it's saved in the DB.

I'm basically trying to take advantage of the power of the Criterion API. To determine if an object meets a criteria.[/i]


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 16, 2005 10:56 pm 
Senior
Senior

Joined: Tue Jun 21, 2005 10:18 am
Posts: 135
Location: South Carolina, USA
Code:
Cat cat1 = session.save(new Cat("snowball",120));

Cat cat2 = session.createCriteria(Cat.class)
.add( Restrictions.like("name", "snow%") )
.add( Restrictions.gt( "weight", new Float(100) ) )
.uniqueResult();


Quote:
Is it possible that cat2 will be equal to cat1 without hitting the database, if all field comparisons are done in the database (no id is available)?


If you are asking whether

Code:
(cat1 == cat2)


would be true, then yes, I believe so, assuming you even get to this point (given the criteria, it is quite possible .uniqueResult() doesn't make sense... there could be a cat named "snowy" that's 130 lbs.)

However, if you are asking if this would avoid a database hit when you ran the criteria, then, no. I'm fairly certain you would end up with the expected database call for the criteria. After all, hibernate itself will need to check to see if any other Cats meet the criteria.

In any case, if I understand what you're trying to do, it's probably a bad use of the criteria API. Criteria is meant for searching the database, so executing a criteria with the hopes it won't hit the database is generally a conflict of interests. A limited exception to this is cached queries, but note that the query cache does not (at least according to Hibernate in Action) cache entities, only entity identifiers and non-entity values.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 16, 2005 11:29 pm 
Beginner
Beginner

Joined: Thu Jun 23, 2005 4:11 pm
Posts: 24
Actually I could have made my example better I should have said:

Cat cat1 = session.save(new Cat("snowball",120));

boolean catMatches = session.createCriteria(Cat.class)
.add( Restrictions.like("name", "snow%") )
.add( Restrictions.gt( "weight", new Float(100) ) )
.matches(cat1);

and catMatches would be true.

I'm thinking a translation could be done such the logic would be:

catMatches = cat1.name.matches("snow*") && cat1.weight > 100.0;

Of course this is a simplistic example and a full implementation of all the Criteria API capabilities would not be possible without hitting the DB.

If I think I understand what you are saying about cached queries it would only help if I knew the id ahead of time and had saved it.

Thanks for the comments though... I'll be sure and rate your posts helpful.


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