• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate DetachedCriteria: how to leave out a given object from the results?

 
Kjeld Sigtermans
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am using Hibernate's DetachedCriteria to retrieve a list of results. Pseudo code looks like this:
The problem I have is: how do I formulate a restriction to leave out a certain object of type Foo from the result list. Such as:
So any result equal to foo is left out?

Currently I use remove(foo) explicitly on the resulting collection, but there must be a way to add this restriction to the query?

Thanks!
Kjeld

 
Arun Kumarr
Ranch Hand
Posts: 661
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kjeld Sigtermans wrote:Hello,

The problem I have is: how do I formulate a restriction to leave out a certain object of type Foo from the result list.



-- What would be your condition to leave out an object. Are you going to leave out foo which has the attribute firstName="James"??
What is your condition going to be?
 
Kjeld Sigtermans
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Arun,

The idea was to have Hibernate compare each result of type Foo with the given object foo, by using the equals method.

I had Hibernate generate the domain classes and I was hoping that Foo would either contain its own implementation of 'equals' method, or that Hibernate would be able to compare two objects foo of type Foo 'under the hood' in some smart way (e.g. comparing each property by reflection).
I guess neither is the case and therefore it is probably not possible to provide DetachedCriteria with an object foo, so that Hibernate will leave out any result for which result.equals(foo) returns true.
The Collection.remove(foo) option does also not work because of the same reason: since Foo does not implement its own version of equals, two objects are compared by hashcode and this obviously fails all the time (I hadn't tried it yet).

So what I did in the end is add a criterion for id comparison:
...which works for me.

Thanks,
Kjeld
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic