tags:

views:

194

answers:

1

This is the problem:

I receive a task for a project which uses Spring, Hibernate and Wicket.

In a particular HTML page I must have the possibility to create a filter (set the name of the filter and its parameters). I must create a list of filters in this way. In the same time, I must have the possibility to edit and delete the filter, and of course, to use that filter.

Any ideas? How could I do this?

My experience with Hibernate is very low, but I need to come with an idea for a project.

My naive solution:

I have the class Table_Something, to which I have associated a table in my database with Hibernate, and I need to create the possibility for the user to create filters for this table in my web application

So, I would create a class, named Table_Something_Filters, to which I will associate also a table in my database with Hibernate. So when, I create a new filter for Table_Something in my application, I would insert a new row in my Table_Something_Filters.

Is this a good idea? Any improvements for my solution?

Another problem: How can I use my filter? I query the Table_Something_Filters to get the values for the parameters of the filter and then what? How can I generate the finder, or how could I query the Table_Something, based on the values from the Table_Something_Filters?

Any ideas or suggestion are welcomed! :)

A: 

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:

public List<MyEntity> 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();

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: name=John;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!

Then, you can dynamically build up a search criteria with only the filled in fields of your Filter class.

If you are using spring with hibernate have a look at their HibernateDaoSupport, and you could also search for the specification pattern which is often used for this complex query building.

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).

HeDinges