Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <pre><code> Criteria criteriaQuery = session.createCriteria(Product.class); Restrictions&lt;Product&gt; restrictions = Restrictions.create(Product.class); Product example = restrictions.getQueryObject(); example.setName(restrictions.like("N%")); example.setPromo("Discounts up to 10%"); restrictions.addRestrictions(criteriaQuery); </code></pre> <p>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. </p> <p>The other problems mentioned in the question are also gone in this framework. Here are example implementations.</p> <pre><code> product.setPrice(restrictions.gt(10)); // price &gt; 10 product.setPromo(restrictions.order(false)); // order by promo desc Restrictions&lt;Manufacturer&gt; 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 */ </code></pre> <p>Even more sophisticated restrictions are available.</p> <pre><code> product.setPrice(restrictions.between(45,55)); product.setManufacturer(restrictions.fetch(FetchMode.JOIN)); product.setName(restrictions.or("Foo", "Bar")); </code></pre> <p>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.</p> <pre><code> restrictions.is(product.getName()).eq("Foo"); restrictions.is(product.getPrice()).gt(10); restrictions.is(product.getPromo()).order(false); </code></pre> <p>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.</p> <p>But wait, there's more!</p> <p>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.</p> <pre><code> Restrictions&lt;Product&gt; restrictions = new Restrictions&lt;Product&gt;(){ public void query(Product queryObject){ queryObject.setPrice(gt(10)); queryObject.setPromo(order(false)); //gt() and order() inherited from Restrictions } } </code></pre> <p>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. <a href="http://sourceforge.net/projects/hqbe2/" rel="noreferrer">http://sourceforge.net/projects/hqbe2/</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload