Wednesday, January 6, 2010

Hibernate Letdowns

As the number of abstraction layers in a software architecture increases, the expressiveness and clarity and of the code increases, but often at the cost of lost functionality. Hibernate has now bitten me twice in this way. First, with the Criteria API's inability to join an entity/ relation more than once to a query and more recently with the inability to access the underlying databases' full set of aggregate functions.

The multiple-join problem ultimately forced me to write my own HqlBuilder class, so that I could maintain some semblance of order in a code base the dynamically creates queries. (Yes, HQL supports these multiple joins, while Criteria queries do not!)

The aggregate function problem prevented the filtering of results from an aggregate query where the aggregated group does not contain a given value in a aggregated field. More precisely, I could not use having every(field<>val) since HQL only supports min, max, and count functions. I figured I could alternately use count(field=val)=0, but alas HQL will not let interesting expressions with the count() function.

Lesson: choose your technology stack carefully! Or as corollary, understand its limitations and how this will impact your ability to meet user requirements.

No comments: