Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1061837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:31:59+00:00 2026-05-16T18:31:59+00:00

I’m in a position where our company has a database search service that is

  • 0

I’m in a position where our company has a database search service that is highly configurable, for which it’s very useful to configure queries in a programmatic fashion. The Criteria API is powerful but when one of our developers refactors one of the data objects, the criteria restrictions won’t signal that they’re broken until we run our unit tests, or worse, are live and on our production environment. Recently, we had a refactoring project essentially double in working time unexpectedly due to this problem, a gap in project planning that, had we known how long it would really take, we probably would have taken an alternative approach.

I’d like to use the Example API to solve this problem. The Java compiler can loudly indicate that our queries are borked if we are specifying ‘where’ conditions on real POJO properties. However, there’s only so much functionality in the Example API and it’s limiting in many ways. Take the following example

 Product product = new Product();
 product.setName("P%");
 Example prdExample = Example.create(product);
 prdExample.excludeProperty("price");
 prdExample.enableLike();
 prdExample.ignoreCase();

Here, the property “name” is being queried against (where name like ‘P%’), and if I were to remove or rename the field “name”, we would know instantly. But what about the property “price”? It’s being excluded because the Product object has some default value for it, so we’re passing the “price” property name to an exclusion filter. Now if “price” got removed, this query would be syntactically invalid and you wouldn’t know until runtime. LAME.

Another problem – what if we added a second where clause:

 product.setPromo("Discounts up to 10%");

Because of the call to enableLike(), this example will match on the promo text “Discounts up to 10%”, but also “Discounts up to 10,000,000 dollars” or anything else that matches. In general, the Example object’s query-wide modifications, such as enableLike() or ignoreCase() aren’t always going to be applicable to every property being checked against.

Here’s a third, and major, issue – what about other special criteria? There’s no way to get every product with a price greater than $10 using the standard example framework. There’s no way to order results by promo, descending. If the Product object joined on some Manufacturer, there’s no way to add a criterion on the related Manufacturer object either. There’s no way to safely specify the FetchMode on the criteria for the Manufacturer either (although this is a problem with the Criteria API in general – invalid fetched relationships fail silently, even more of a time bomb)

For all of the above examples, you would need to go back to the Criteria API and use string representations of properties to make the query – again, eliminating the biggest benefit of Example queries.

What alternatives exist to the Example API that can get the kind of compile-time advice we need?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T18:32:00+00:00Added an answer on May 16, 2026 at 6:32 pm

    My company gives developers days when we can experiment and work on pet projects (a la Google) and I spent some time working on a framework to use Example queries while geting around the limitations described above. I’ve come up with something that could be useful to other people interested in Example queries too. Here is a sample of the framework using the Product example.

     Criteria criteriaQuery = session.createCriteria(Product.class);
    
     Restrictions<Product> restrictions = Restrictions.create(Product.class);
     Product example = restrictions.getQueryObject();
     example.setName(restrictions.like("N%"));
     example.setPromo("Discounts up to 10%");
    
     restrictions.addRestrictions(criteriaQuery);
    

    Here’s an attempt to fix the issues in the code example from the question – the problem of the default value for the “price” field no longer exists, because this framework requires that criteria be explicitly set. The second problem of having a query-wide enableLike() is gone – the matcher is only on the “name” field.

    The other problems mentioned in the question are also gone in this framework. Here are example implementations.

     product.setPrice(restrictions.gt(10)); // price > 10
     product.setPromo(restrictions.order(false)); // order by promo desc
     Restrictions<Manufacturer> manufacturerRestrictions 
            = Restrictions.create(Manufacturer.class);
     //configure manuf restrictions in the same manner...
     product.setManufacturer(restrictions.join(manufacturerRestrictions)); 
     /* there are also joinSet() and joinList() methods
      for one-to-many relationships as well */
    

    Even more sophisticated restrictions are available.

     product.setPrice(restrictions.between(45,55));
     product.setManufacturer(restrictions.fetch(FetchMode.JOIN));
     product.setName(restrictions.or("Foo", "Bar"));
    

    After showing the framework to a coworker, he mentioned that many data mapped objects have private setters, making this kind of criteria setting difficult as well (a different problem with the Example API!). So, I’ve accounted for that too. Instead of using setters, getters are also queryable.

     restrictions.is(product.getName()).eq("Foo");
     restrictions.is(product.getPrice()).gt(10);
     restrictions.is(product.getPromo()).order(false);
    

    I’ve also added some extra checking on the objects to ensure better type safety – for example, the relative criteria (gt, ge, le, lt) all require a value ? extends Comparable for the parameter. Also, if you use a getter in the style specified above, and there’s a @Transient annotation present on the getter, it will throw a runtime error.

    But wait, there’s more!

    If you like that Hibernate’s built-in Restrictions utility can be statically imported, so that you can do things like criteria.addRestriction(eq(“name”, “foo”)) without making your code really verbose, there’s an option for that too.

     Restrictions<Product> restrictions = new Restrictions<Product>(){
           public void query(Product queryObject){
             queryObject.setPrice(gt(10));
             queryObject.setPromo(order(false));
             //gt() and order() inherited from Restrictions
           }            
     }
    

    That’s it for now – thank you very much in advance for any feedback! We’ve posted the code on Sourceforge for those that are interested. http://sourceforge.net/projects/hqbe2/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have an array which has BIG numbers and small numbers in it. I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a small JavaScript validation script that validates inputs based on Regex. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.