Unable to convert java.sql.Timestamp to com.google.cloud.Timestamp in Spring Data Cloud Spanner

别等时光非礼了梦想. 提交于 2021-01-29 13:56:45

问题


While using Spring Data Cloud Spanner, we are unable to persist java.sql.Timestamp type data in spanner db. The database column type is Timestamp.

Following is the entity:

@Table(name = "TEST")
public class Test {
  
  @PrimaryKey
  @Column(name = “ID”)
  private String id;
 
  @Column(name = "CREATED_ON")
  private java.sql.Timestamp createdOn;

}

We are using Spring Data Rest to persist.

As per my analysis, I see a converter is already there which converts java.sql.TimeStamp to com.google.cloud.Timestamp.

public static final Converter<java.sql.Timestamp, Timestamp> JAVA_TO_SPANNER_TIMESTAMP_CONVERTER =
                    new Converter<java.sql.Timestamp, Timestamp>() {
                        // @formatter:on
                        @Nullable
                        @Override
                        public Timestamp convert(java.sql.Timestamp timestamp) {
                            return Timestamp.of(timestamp);
                        }
                    };

    /**
     * A converter from the Spanner instantaneous time type to {@link java.sql.Timestamp}.
     */
    // @formatter:off
    public static final Converter<Timestamp, java.sql.Timestamp> SPANNER_TO_JAVA_TIMESTAMP_CONVERTER =
                    new Converter<Timestamp, java.sql.Timestamp>() {
                        // @formatter:on
                        @Nullable
                        @Override
                        public java.sql.Timestamp convert(Timestamp timestamp) {
                            return java.sql.Timestamp.from(TIMESTAMP_INSTANT_CONVERTER.convert(timestamp));
                        }
                    };

Instead of that while searching for perfect match from source type to target type, it is getting a perfect match between java.sql.Timestamp to com.google.cloud.Date because of this following code snippet.

public GenericConverter find(TypeDescriptor sourceType, TypeDescriptor targetType) {
            // Search the full type hierarchy
            List<Class<?>> sourceCandidates = getClassHierarchy(sourceType.getType());
            List<Class<?>> targetCandidates = getClassHierarchy(targetType.getType());
            for (Class<?> sourceCandidate : sourceCandidates) {
                for (Class<?> targetCandidate : targetCandidates) {
                    ConvertiblePair convertiblePair = new ConvertiblePair(sourceCandidate, targetCandidate);
                    GenericConverter converter = getRegisteredConverter(sourceType, targetType, convertiblePair);
                    if (converter != null) {
                        return converter;
                    }
                }
            }
            return null;
        }

This method returns a converter for source type java.sql.Timestamp which is actually responsible for converting java.util.Date to com.google.cloud.Date as java.util.Date is super class of java.sql.Timestamp.


回答1:


The issue was fixed in this PR https://github.com/spring-cloud/spring-cloud-gcp/pull/2065 and the fix is available in 1.3.0.BUILD-SNAPSHOT. Can you please try and give us a feedback.

Thanks,



来源:https://stackoverflow.com/questions/59136904/unable-to-convert-java-sql-timestamp-to-com-google-cloud-timestamp-in-spring-dat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!