Hibernate Criteria Api

hibernate

Criteria criteria = session.createCriteria(UserDetails.class);
criteria.add(Restrictions.eq("userName","User 10");
List<UserDetails> users = (List<UserDetails>) criteria.list();

Restrictions.ge
.gt
.like
.between

// Criteria API is for dealing with complex queries.

// The projection part of the Criteria API is for dealing with aggregation.

Criteria criteria = session.createCriteria(UserDetails.class)
  .setProjection(Projections.property("userId"));
// The above code returns all rows, but with just the userId column.

Criteria criteria = session.createCriteria(UserDetails.class)
  .setProjection(Projections.max("userId"));

Projections.max
.count
.avg
.property

criteria.addOrder(Order.desc("userId"))

// Query by example:
UserDetails exampleUser = new UserDetails();
sampleUser.setUserId(5);
sampleUser.setUserName("User 5");
Example example = Example.create(exampleUser);
Criteria criteria = session.createCriteria(UserDetails.class)
  .add(example);
// When we do query by example, Hibernate ignores the primary key, and the
// member variables that are null.
// Query by example is handy when we have a long list of member variables
// that we need to pass to the query.  This helps reduce the number of
// Restrictions that we have to add.

UserDetails exampleUser = new UserDetails();
sampleUser.setUserName("User %");
Example example = Example.create(exampleUser).enableLike();
Criteria criteria = session.createCriteria(UserDetails.class)
  .add(example);
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License