question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Same type (java.sql.Timestamp) turns into different SQL-types

See original GitHub issue

Hello, I am not sure where this happens, but I am having some trouble with ZonedDateTime.

I have read in the documentation that ZonedDateTime is not supported (https://docs.spring.io/spring-data/jdbc/docs/current/reference/html/#jdbc.entity-persistence.types), therefore I wrote a converter (ZonedDateTime -> java.sql.Timestamp).

I have the following data class:

data class OrderLineEvent(
  (...) other fields 
  val orderLineEventType: String? = null,
  val travelDate: ZonedDateTime? = null, 
  val timestamp:  LocalDateTime = LocalDateTime.now() // Actually a ZonedDateTime, but using LocalDateTime for illustration purposes 
) {
}

Converter:

import java.sql.Timestamp
import java.time.ZoneOffset
import java.time.ZonedDateTime

@Configuration
@EnableJdbcRepositories
class DataJdbcConfiguration : AbstractJdbcConfiguration() {

  @WritingConverter
  class ZonedDateTimeWriterConverter : Converter<ZonedDateTime, Timestamp> {

    override fun convert(date: ZonedDateTime): Timestamp {
      return Timestamp.valueOf(date.withZoneSameInstant(ZoneOffset.UTC).toLocalDateTime())
    }

  }

  @Bean
  override fun jdbcCustomConversions(): JdbcCustomConversions {
    return JdbcCustomConversions(listOf(ZonedDateTimeWriterConverter()))
  }
}

Repository:

@Repository
interface OrderLineEventRepository : CrudRepository<OrderLineEvent, Long>

Ok, here is the issue: I am unable to save this as a timestamp. The error is Translating SQLException with SQL state '42804', error code '0', message [ERROR: column "travel_date" is of type timestamp without time zone but expression is of type character varying which is quite odd.

After investigating this further (and enabling TRACE-logging), it seems like the parameter gets converted into a VARCHAR/string type(?) AND a different type than the `LocalDateTime

Look at logs below:

o.s.jdbc.core.StatementCreatorUtils      : Setting SQL statement parameter value: column index 11, parameter value [123.42], value class [java.lang.String], SQL type 12  
o.s.jdbc.core.StatementCreatorUtils      : Setting SQL statement parameter value: column index 12, parameter value [2021-11-17 08:31:28.731541], value class [java.sql.Timestamp], SQL type 93  
o.s.jdbc.core.StatementCreatorUtils      : Setting SQL statement parameter value: column index 13, parameter value [2021-11-17 08:31:28.73009], value class [java.sql.Timestamp], SQL type 12  

It seems like String and ZonedDateTime turns into the same SQL type.

String -> java.lang.String, SQL type 12
LocalDateTime -> java.sql.Timestamp, SQL type 93
ZonedDateTime -> java.sql.Timestamp, SQL type 12

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
schaudercommented, Nov 18, 2021

I don’t consider this workaround a permanent solution. And I understand where the problem with null comes from, basically null gets a shortcut and doesn’t use conversions, but apart from avoiding nulls I don’t have a workaround at hand.

1reaction
olereidarcommented, Nov 18, 2021

Thank you, this works. Do you have any plans on fixing this, or do you consider this workaround a permanent solution?

EDIT: It only works partly. If the value is nullable (which it is in this case) it crashes when inserting null. Same error: is of type timestamp without time zone but expression is of type character varying

o.s.jdbc.core.StatementCreatorUtils      : Setting SQL statement parameter value: column index 3, parameter value [null], value class [null], SQL type 12 

Any ideas on workaround here?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is java.sql.Timestamp timezone specific? - Stack Overflow
I have converted the dateTime given in specific timezone to UTC. for that I followed the below code. My input dateTime is "20121225...
Read more >
Date, time, and timestamp values that can cause problems in ...
Timestamp for TIMESTAMP SQL types. When you assign a string value to a DATE, TIME, or TIMESTAMP target, the IBM Data Server Driver...
Read more >
Timestamp (Java Platform SE 8 ) - Oracle Help Center
Converts a String object in JDBC timestamp escape format to a Timestamp value. Methods inherited from class java.util.Date · after, before, clone, getDate, ......
Read more >
Untitled
Even when different databases support SQL types with the same semantics, ... JDBC defines a standard mapping from the JDBC database types to...
Read more >
Data types | BigQuery - Google Cloud
Values of the same comparable data type can be compared to each other. All data types are ... To represent an absolute point...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found