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.