tags:

views:

143

answers:

1

A Spring JDBC question: I use a lot of objects as bind variables which are not automatically mapped to their JDBC equivalents, e.g. jdbctemplate.query(sql, foo, bar, wee) with foo, bar and wee being instances of objects which are not covered by the JDBC type autoconversion.

Can anyone on SO suggest a best practice for a conversion strategy for such cases? Most cases would boil down to call toString() on the instances anyways.

A: 

I'm not sure that I understand the question, but I believe there are two parts to this binding and conversion problem:

  1. Binding from UI to Java objects is handled using the Spring data binding API. That can handle binding to both standard Java types and your own custom types (if you write the appropriate PropertyEditors).
  2. Binding your Java objects to JDBC depends on the underlying implementation. If you're using Spring Simple JDBC, you'll write ParameterizedRowMappers to get data out of ResultSets and into your objects. If you're using Hibernate, you'll have ORM mapping files. iBatis has its own way to manage it.

I don't understand what "Most cases would boil down to call toString() on the instances anyways" means.

duffymo
Basically I want a conversion strategy for bind variables, not for result sets. `JdbcTemplate` bind variables are usually supplied as `Object` vararg. These variables must be part of the set of supported JDBC types. What I'd like is some kind of pluggable conversion strategy that converts my custom bind variables (e.g. instances of a class `Foo`) to JDBC types (e.g. `java.sql.Types.VARCHAR`)
yawn