As a developer I need to write fairly complex SQL queries dynamically based on user input. A typical example is when user defined filter criteria needs to be converted into a WHERE clause. One option is to use Criteria API, but for those who prefer to hand craft their SQL statements a dynamic SQL generation similar to MyBatis Dynamic SQL would be useful. With this feature an SQL statement could be defined like this :
<sql-query name="persons"> <return alias="person" class="eg.Person"/> SELECT person.NAME AS {person.name}
, person.AGE AS {person.age}
, person.SEX AS {person.sex}
FROM PERSON person <#if namePattern??> WHERE person.NAME LIKE :namePattern </#if> </sql-query>
and the query would be executed in the usual way
List people = sess.getNamedQuery("persons") .setString("namePattern", namePattern) .setMaxResults(50) .list();
The template engine could be FreeMarker or Velocity but it could be configurable through persistence.xml or annotation.
Would this bring value to the project? If so I am happy to contribute it in the form of a pull request.
|