We're currently researching the best way to upgrade from Toplink 2.1-60f to EclipseLink 2.6. The project is somewhat large and most of the manual work would have to be done in parts of the code where we are using NativeQuery. Query.getResultList() result differs between the two JPA-implementations as TopLink returns a List<Vector> and EclipseLink on the other hand returns a List<Object[]>. The code is unfortunately therefore littered with List<Vector> references.
Part of the solution would be to convert the result from list array to a list of vectors. Instead of doing this in all the numerous places manually, I was thinking we could use AspectJ to intercept the getResultList() calls and convert the return values. Is this a viable solution? Has anyone implemented similar solutions? We're using Maven as our build tool.
Thanks in advance!
My suggestion is: Use a good IDE and refactor your code!
But because you asked for an AOP solution, here is a self-consistent AspectJ example. As I have never used JPA, I will just recreate your situation as a little abstraction.
Abstract
Querybase implementation with lots of dummy methods:The only method missing is
getResultList(), so now let us provide two different implementations for it, extending the abstract base implementation:Concrete
Queryimplementation returningList<Vector>:This emulates your TopLink class.
Concrete
Queryimplementation returningList<Object[]>:This emulates your EclipseLink class.
Driver application:
The application creates queries of both concrete subtypes, each time assuming that the list elements will be vectors.
Console log without aspect:
Uh-oh! This is exactly your problem, right? Now what can we do about it if we absolutely refuse to refactor? We abuse AOP for patching up the legacy code. (Please don't do it, but you can if you absolutely want to.)
AspectJ query result adapter:
Disregarding usage of raw types and other ugly stuff, here is my proof of concept:
Console log with aspect:
Et voilà - you can do ugly stuff and other things it was not invented for with AOP.
;-)