• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate: Excluding values

 
Ergin Er
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Is it possible in hibenate, using Criteria, to exclude some records (given by id)?
I'm using Example to list all the records.
Criteria criteria = session.createCriteria(POJO.class);
criteria.add(Example.create(pojo));

And I need to do criteria.add(Exclude(pojo.getId())) or criteria.add(not("id", pojo.getId) or something like that. I know that with using .add(Expression) I can add statements with "not equal to" part but just wondered if there was an easier way.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I know that with using .add(Expression) I can add statements with "not equal to" part but just wondered if there was an easier way.

I don't know if there could be an easier way . If you use Criteria you have all the restrictions available to you through the Restrictions class, which includes "not". Alternatively you could use HQL (or even SQL) and exclude that way.
 
Ergin Er
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Sturrock:

I don't know if there could be an easier way . If you use Criteria you have all the restrictions available to you through the Restrictions class, which includes "not". Alternatively you could use HQL (or even SQL) and exclude that way.


Thanks for your reply. I didn't know about the restrictions class. I think that will be very usefull from now on
I was a bit worried because the criteria had to exclude several id's from the result list. So that meant (when using HQL) adding or statements a lot. And using or with criteria might be doable for 2 values but if that number goes up, I would loose the oversight. But I guess restriction class takes care of this.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic