What is the current best practice for all the type conversions necessary in a Java web application? For example, HttpServletRequest.getParameters(...) returns String[], but Hibernate does not allow String[] to be used in an IN clause for a numeric column. Therefore I need to convert String[] to Long[] via Long.valueOf(). What is the best way to handle this, short of rewriting in another language? Do people just create a class full of little static methods to do this?
|
|
||||
|
|
|
If you use a JAX-RS implementation like Jersey, you'll get much improved parameter handling and could map it directly to a List. If you don't want to go that far, you could also pull tricks like using Guava |
|||
|
Seriously? Just because all those hip new languages have a compact syntax for transforming lists/arrays, it doesn't mean using a loop will infect you with a terminal disease or anything.
There, that wasn't so bad, was it? And yeah, you can and should put it in a static helper method if you use it in several places. Edit: Since Hibernate can also take collections, a somewhat cleaner version:
|
|||||||||||||
|
|
Apache Commons NumberUtils is best. The conversion routines in the JDK like to just throw a |
|||||||||||
|