Archives for: February 2006, 03

Feb 03, 2006 : a good idea : use Java/.NET code to define your ORM query

Native Queries would have you implement a match(Object o) method in Java that is then used to create a SQL select statement. Byte-code analysis of your method is used to convert the method's logic into a query usable your favorite ORM library. What's great about this is that you don't have to learn a new query language, and that it's all checked at compile-time and you can even do IDE refactoring.

As an example, HQL (the hibernate query language) is parsed, etc at runtime so syntax or variables are not checked until runtime. Hibernate has "criteria queries" that avoid the issue of syntax by having classes/methods that represent all the different possible operators. But you still have to pass in properties as strings (e.g."firstName", nameQuery)).

Implementing native queries could be difficult, but even a simple implementation that permits only very basic operations could cover a vast majority of cases. For example, permit basic operators and the use of methods and variables in the model class, but don't permit execution of any other code. If your query logic was complex you wouldn't want to use this approach anyway but would directly use the ORM query language or SQL.

It looks like the POJQ library has an implementation for JDO and could be extended for other ORM libraries. There is also a db4o project which has a much broader scope and seems somewhat "closed" although under an open license. The API requires a login to find, so if you're curious you can find it here.

Via Dr. Dobb's Journal. Covered on TSS a while ago too.

<  February 2006  >
Mon Tue Wed Thu Fri Sat Sun
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28          




Syndicate this blog XML

powered by