title: Dynamic Query on multiple properties with Hibernate
body: <p>I have a model that has several properties. The properties can be primitive (String) or complex (Object). The user can make a query on each primitive property. I would like to know if there is an easy way to build dynamically the query. I use Java and Hibernate.</p>
  
  <p>The model</p>
  
  <pre><code>public class Model {
    String prop1;
    Point prop2;
    List&lt;Shape&gt; prop3;
  }
  </code></pre>
  
  <p><code>Point</code> and <code>Shape</code> are object that can contains primitives or objects. An example of a query would be all instances where prop1 = "A" and the coordinates are x = 3 and y = 8 and one of the shape is a circle.</p>
  
  <p>prop1 = "A" and prop2.x = 3 and prop2.y and prop3.get(i).type = "Circle"; we would have to iterate on all instances of prop3.</p>
  
  <p>My first idea was unmaintainable and inefficient. It consists in doing queries on all the primitive properties and then merge the results.</p>
  
  <ul>
  <li>Get all instances where prop1 = "A"</li>
  <li>Get all instances where prop2.x = 3
  and prop3 = y;</li>
  <li>Get all instances where one of the
  Shape.type = "Circle";</li>
  <li>Get the intersection of all 3 sets</li>
  </ul>
  
  <p>Is there any existing library or algorithm that can solve this problem in a better (smarter) way?</p>
  
  <p>Thanks</p>