Querying with criteria and (Make web site) example The Criteria API

Querying with criteria and example The Criteria API and the org.hibernate.criterion package offer many other operators besides eq() you can use to construct more complex expressions. Creating comparison expressions All regular SQL (and HQL, JPA QL) comparison operators are also available via the Restrictions class: Criterion restriction = Restrictions.between(”amount”, new BigDecimal(100), new BigDecimal(200) ); session.createCriteria(Bid.class).add(restriction); session.createCriteria(Bid.class) .add( Restrictions.gt(”amount”, new BigDecimal(100) ) ); String[] emails = { “foo@hibernate.org”, “bar@hibernate.org” }; session.createCriteria(User.class) .add( Restrictions.in(”email”, emails) ); A ternary logic operator is also available; this query returns all users with no email address: session.createCriteria(User.class) .add( Restrictions.isNull(”email”) ); You also need to be able to find users who do have an email address: session.createCriteria(User.class) .add( Restrictions.isNotNull(”email”) ); You can also test a collection with isEmpty(), isNotEmpty(), or its actual size: session.createCriteria(Item.class) .add( Restrictions.isEmpty(”bids”)); session.createCriteria(Item.class) .add( Restrictions.sizeGt(”bids”, 3)); Or you can compare two properties: session.createCriteria(User.class) .add( Restrictions.eqProperty(”firstname”, “username”) ); The criteria query interfaces also have special support for string matching. String matching For criteria queries, wildcarded searches may use either the same wildcard symbols as HQL and JPA QL (percentage sign and underscore) or specify a MatchMode. The MatchMode is a convenient way to express a substring match without string manipulation. These two queries are equivalent:
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.

Leave a Reply