Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excluding a bean's field in Hibernate criteria result

Here is how I'm getting my User beans from Database.

session.createCriteria(User.class).list();

That returns all of User records from database. Interesting part is that, I don't want to get the password field from DB. Just want to exclude that field while retrieving.

Options I have

1) using projections on other fields. That needs to be more code to add in to projections list. So dropped from that idea.

2) With Sql I need to write a manually query, which kills the theme of Hibernate.

Any possibility to exclude a column value of Bean ?

like image 772
Suresh Atta Avatar asked Dec 08 '22 06:12

Suresh Atta


1 Answers

you can try:

Example example = Example.create(cat)
    .excludeZeroes()           //exclude zero valued properties
    .excludeProperty("color")  //exclude the property named "color"
    .ignoreCase()              //perform case insensitive string comparisons
    .enableLike();             //use like for string comparisons
List results = session.createCriteria(Cat.class)
    .add(example)
    .list();

Reference :-

  • http://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/querycriteria.html#querycriteria-examples
  • http://docs.jboss.org/hibernate/orm/3.6/javadocs/org/hibernate/criterion/Example.html
like image 142
Vipul Paralikar Avatar answered Jan 11 '23 00:01

Vipul Paralikar