liveBook · Manning

Java.sql temporal types

As explained in Java Persistence with Spring Data and Hibernate epub

The java.sql temporal types are JDBC date, time, and timestamp classes corresponding to SQL temporal types.

The JDBC temporal classes correspond to SQL DATE, TIME, and TIMESTAMP: java.sql.Date, java.sql.Time, and java.sql.Timestamp. They are also subclasses of java.util.Date. section 6.3.1

A java.util.Date property can come back from Hibernate as any of these JDBC subclasses, depending on its TemporalType mapping. In particular, Timestamp can retain database nanoseconds that java.util.Date cannot represent, making equals() comparisons unreliable across the two types. Prefer java.time classes for new domain-model properties; JPA 2.2 supports them. section 6.3.1

Comparing loaded values

If nanoseconds do not matter, compare getTime() values rather than calling equals() on mixed java.util.Date and JDBC temporal values. Avoid mixing those values in a HashSet, which uses equals(). section 6.3.1

Table 6.3 Date and time types (view table figure)

Name

Java type

ANSI SQL type

date

java.util.Date, java.sql.Date

DATE

time

java.util.Date, java.sql.Time

TIME

timestamp

java.util.Date, java.sql.Timestamp

TIMESTAMP

calendar

java.util.Calendar

TIMESTAMP

calendar_date

java.util.Calendar

DATE

duration

java.time.Duration

BIGINT

instant

java.time.Instant

TIMESTAMP

localdatetime

java.time.LocalDateTime

TIMESTAMP

localdate

java.time.LocalDate

DATE

localtime

java.time.LocalTime

TIME

offsetdatetime

java.time.OffsetDateTime

TIMESTAMP

offsettime

java.time.OffsetTime

TIME

zoneddatetime

java.time.ZonedDateTime

TIMESTAMP

Compare the JDBC classes’ SQL mappings with Calendar and java.time alternatives. section 6.3.1

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Explains Hibernate’s returned subclasses, timestamp precision, and equality pitfalls without detailing JDBC internals.
Examples
Uses getTime() comparisons and HashSet behavior to illustrate mixed-type equality risks.
What you can do after
Diagnose/debug surprising loaded date types and unsafe comparisons; choose java.time for new properties.
How it connects
Connects TemporalType mappings with java.util.Date, java.time, Hibernate, and SQL temporal columns.

This explainer was generated by Manning's knowledge graph from the text of Java Persistence with Spring Data and Hibernate epub. The section links point to the parts of the book it draws on.