开发者

Elegant way to implement a criteria-based search page in Hibernate

Using Hibernate how would you design and implement a search criteria page (which has multiple editable/selectable fields/drop-downs as search criteria) such that queries shouldn't clutter data accessor code. I mean no query-string concatenation based on conditionals and ultimately all the queries should go in a separate xml file. I've done such an implementation using IBatis's dynamic queries. Co开发者_运维技巧uldn't find such thing in Hibernate so I started thinking what would be the elegant way to implement dynamic criteria based page in hibernate.


Sounds like you are looking for, unsurprisingly, the Criteria API:

http://docs.jboss.org/hibernate/core/3.5/reference/en/html/querycriteria.html


as I had the same issue, I developed a Generic Dao class that allows dynamically(using reflection) to create the criteria based on the values assigned to the object and query the database

e.g Country country = new Country();
// these values lets say they were assigned on your servlet based on the user post country.setName("Luxembourg");

        // This is where your service layer starts. It gets as a param the Country object
        GenericDaoDB gDaoDB = new GenericDaoDB();

        try{
            List resultList = gDaoDB.list(country);
            System.out.println("=========Result Print==============");
            for(int i=0; i<resultList.size();i++){
                 Country resultCountry = (Country)resultList.get(i);
                 System.out.println("Name:"+ resultCountry.getName()+"  Country Code:"+resultCountry.getCountryCode());
             }
        }catch(BasisException e){
            e.printStackTrace();
        }

If you like have a look on http://sourceforge.net/apps/wordpress/jprovocateur/2010/09/23/simple-example-hibernate-query-the-database-without-hql-or-criteria/ where you can find more details and the example project.

And as it is a generic class you can use it for all your Pojos


I second Affe's suggestion, the Criteria API is exactly what you're looking for and is recommended when dealing with dynamic queries. This is very nicely illustrated in Hibernate Querying 102 : Criteria API that I'm quoting below:

Using the Hibernate Criteria API

The Hibernate Criteria API provides an elegant way of building on-the-fly dynamic queries on Hibernate-persisted databases. Using this technique, the previous 24-line example can be coded more consicely and more clearly using a mere 8 lines of code :

Criteria criteria = session.createCriteria(Sale.class);
if (startDate != null) {
  criteria.add(Expression.ge("date",startDate);
}
if (endDate != null) {
  criteria.add(Expression.le("date",endDate);
}
List results = criteria.list();

Let's have a look at the use of the Hibernate Criteria API in more detail.

The code shown in the article speaks for itself, just have a look.

Related questions

  • Hibernate: Criteria vs. HQL

Resources

  • Hibernate Querying 102 : Criteria API
  • Hibernate Criteria API: Multi-Criteria Search Made Easy
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜