Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Spring Data JDBC support custom type converters

I have an entity that has a filed of type java.util.Date (or Timestamp, doesn't matter for the case).

public class StatusReason {
    @Column("SRN_ID")
    private Long id;
    @Column("SRN_CREATOR")
    private String creator;
    @Column("SRN_REASON")
    private String reason;
    @Column("SRN_STATUS")
    private String status;
    @Column("SRN_TIMESTAMP")
    private Date timestamp;
//getters, setters, etc...
}

The database is Oracle and the corresponding column is of type TIMESTAMP(6) WITH TIME ZONE

When I call any of the default findById or findAll methods of the repository I get: ConverterNotFoundException: No converter found capable of converting from type [oracle.sql.TIMESTAMPTZ] to type [java.util.Date].

I can create a custom RowMapper for the type and it will work fine. I was just wondering if it's possible to register a custom converter (in my case from oracle.sql.TIMESTAMPTZ to java.util.Date) so can still benefit from the default mapping and use the converter through the whole app.

like image 382
Evgeni Dimitrov Avatar asked Nov 02 '18 14:11

Evgeni Dimitrov


People also ask

Does Spring support JDBC?

The Spring Framework's JDBC abstraction framework consists of four different packages, namely core , datasource , object , and support .

What is the difference between Spring JDBC and Spring Data JPA?

Spring JDBC only helps with the connection to the database and with executing queries on this database. Spring Data JPA wants to help you manage your JPA based repositories. It wants to remove boilerplate code, make implementation speed higher and provide help for the performance of your application.

What is Spring Data JDBC?

Spring Data JDBC is an object-relational mapping framework for relational databases that aims to avoid most of the complexity of other ORM frameworks. It does that by avoiding features like lazy loading, managed lifecycles of entity objects and caching.

How does Spring integrate with JDBC?

Create a project with a name SpringExample and create a package com. tutorialspoint under the src folder in the created project. Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter. Add Spring JDBC specific latest libraries mysql-connector-java.


1 Answers

You can register custom conversions by inheriting your configuration from JdbcConfiguration (Spring Data JDBC v1.x) or AbstractJdbcConfiguration (v2.x). Then overwrite the method jdbcCustomConversions().

JdbcCustomConversions takes a list of Converter as an argument.

like image 80
Jens Schauder Avatar answered Oct 17 '22 06:10

Jens Schauder