Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Well, yes, you could save in your *_FILTER table the parameters and values the user entered as a filter and then in your DAO have a method like this:</p> <pre><code>public List&lt;MyEntity&gt; findByFilter(MyEntityFilter filter) { //Use hibernate criteria api to build up your query depending on value in your filter Criteria criteria = session.createCriteria(MyEntity.class) if (filter.getName() != null) { criteria.add( Restrictions.like("name", filter.getName()) } ... Go on testing and adding other parts of the query return criteria.list(); </code></pre> <p>The MyEntityFilter class would be a simple class with getters and setters for each parameter of your possible search filter. This Filter class you would have build it up with the values you stored in your database. If filters don't often change you can cache them in your application. If you have only a few filterable parameters you can store them in separate columns, else you would rather store a string like this: <em>name=John</em>;color=blue,red,yellow;parameter3=value* and then parse the string to fill up your Filter objects. Be careful about your separator chars you use, you should escape them from your user input!</p> <p>Then, you can dynamically build up a search criteria with only the filled in fields of your Filter class.</p> <p>If you are using spring with hibernate have a look at their <a href="http://static.springsource.org/spring/docs/2.5.6/reference/orm.html#orm-hibernate" rel="nofollow noreferrer">HibernateDaoSupport</a>, and you could also search for the specification pattern which is often used for this complex query building.</p> <p>Off course you don't have to use the criteria api, you could build up a HQL query string also, but the criteria api is more appropriate for this kind of stuff (not always more easy to read though).</p>
 

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