• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

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

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 662
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Think of how dumb the average person is. Mathematically, half of them are EVEN DUMBER. Smart tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic