Announcement Announcement Module
Collapse
No announcement yet.
Using non-standard data types with BeanPropertySqlParameterSource & named parameters Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using non-standard data types with BeanPropertySqlParameterSource & named parameters

    We have a number of data objects. The objects and the database use a java.util.UUID as a surrogate keys. In the database, the data type is a char(36). When we attempt to use a BeanPropertySqlParameterSource along with a Named Parameter operations, a "incompatible data type in conversion" Exception is thrown. This is obviously because the UUID object cannot be mapped directly to an SQL data type. The UUID class has a standard toString() method that has the value we want. But since it is not a "getter", we cannot use dot notation in the sql (i.e. we cannot use :key.string or :key.toString).

    Given the number of data objects we have, and the number of properties on some of them, we would really like to use the BeanPropertySqlParameterSource rather than having to create a MapSqlParameterSource and individually add all the properties.

    Is there a way to handle non stabdard data types in a simple manner within the Spring JDBC APIs? Or will we need to either write a customized BeanPropertySqlParameterSource, or added a second getter to our DataObjects that get any UUIDs as Strings and use that "property" in the named parameter syntax?

    Sample code:


    Code:
    sqlStatement = "INSERT INTO table_name 
                   (key, foreign_key_to_other, value_one, value_two) 
                   values 
                   (:key, :foreignKeyToOther, :valueOne, :valueTwo)";
    sqlParameterSource = new BeanPropertySqlParameterSource(dataObject)
    simpleJdbcTemplate.getNamedParameterJdbcOperations().update(sqlStatement, sqlParameterSource);
    
    
    import java.util.UUID;
    
    public class DataObject
    {
        private UUID key;
        
        private UUID foreignKeyToOther;
        
        private String valueOne;
        
        private int valueTwo;
                
        //Setters and getters for above
    }
Working...
X