Saturday, September 14, 2013

JPA Dynamic Query Builder


Dynamic Query Builder

Every time i write a JPA project i always face the need to dynamically load my entities from database without writing the HQL by myself, and for that i decided to write my own Dynamic Query Builder - DQB - which can be plugged as a jar to any new project.

this DQB will allow me to specify what to select and from what entity, it will allow to specify grouped filters, aggregation , grouping, ordering in a freindly and easy way, and after two days of coding i came up with the following DQB:

Setup

(this setup assumes you are using spring and you have an entityManager defined in your spring context), and also before we start i have to mention that this DQB is for use with simple scenarios and for unpredictable query parameters and we not to forget that JPA already have a powerful Criteria builder APIs and also there is the named queries, so this DQB is is to be used for small projects or  as a mix with above, and at the ends what we need decides what to use.

first you need to download the jar and add it to your class path
download link : DynamicQueryBuilder
the source code is also available for those who wants to toy with the jar and may need to add any feature or fix a bug.

then in your spring context xml file you need to add the following bean:


and thats it. now we are ready to use the query generator.

Obtaining QueryGenerator instance

in any part of your code you refrence your spring context and get the DQB instance like this :



The usage


lets assume we have an entity name SampleEntity :





Available Filters can be found under the package :
akab.bo3.dqb.query.filter

Other functionality :
groupBy, orderBy, aggregate

Other Links :

Source code and Sample project : SampleProject
SampleProject Libs : libs

6 comments:

  1. more usage example will be added later.
    i wish this will help as many as possible and i will appreciate any comment, reported bug, feature suggestion and any question.

    ReplyDelete
  2. Nice, clean and to the point working solution...keep up bro.

    ReplyDelete
    Replies
    1. thanx, and it would be very helpfull if i get any feedback back from your usage of my DQB.

      Delete
  3. Interesting Job.

    However, I think in next version, it would be helpful if you add pagination feature. because, when it used with big data it will crash the system memory.

    Another point you should mention that this framework better to used only in case of unknown query parameters such as search form with unpredictable search criteria. Otherwise, you should use Named Query to get better performance.

    Keep this great momentum in writing.
    Thank you once again.....

    ReplyDelete
    Replies
    1. thank you very much for the comment, and regarding the pagination it has it from the first version but it was not mentioned in the examples of usage so i added the example as the last one today, later ill be adding a java docs to my post so the full features list will be listed,

      and yes using named queries has better performance and this DQB is for use with small projects and when have unpredictable queries parameters, and we need not to to forget that JPA has a powerful criteria builder APIs so deciding what to use depends on the needs.

      Delete