package com.ankurm.hibernatedemo.datetime; import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManagerFactory; import java.time.LocalDateTime; import java.time.OffsetDateTime; import java.time.ZoneOffset; import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.TestPropertySource; /** * Backs ankurm.com post 4876, docs/13-date-and-time-mapping.md chapter "hibernate.jdbc.time_zone". * Sets {@code hibernate.jdbc.time_zone=America/New_York} (JVM default left at whatever the * build runs under) and observes: (1) what it does to a {@code LocalDateTime} (no offset of * its own -- the JDBC time zone controls what wall-clock value is actually sent to the driver), * and (2) whether it changes anything for a {@code TimeZoneStorage.NATIVE} OffsetDateTime, * which already carries its own explicit offset. */ @SpringBootTest @TestPropertySource(properties = { "spring.jpa.properties.hibernate.jdbc.time_zone=America/New_York" }) class JdbcTimeZoneTest { private static final Logger DEMO = LoggerFactory.getLogger("DEMO"); @Autowired private EntityManagerFactory emf; @Test void jdbcTimeZone_affectsLocalDateTime_doesNotAffectExplicitOffset() { LocalDateTime localDateTime = LocalDateTime.of(2026, 7, 4, 9, 0, 0); OffsetDateTime explicitOffset = OffsetDateTime.of(2026, 7, 4, 9, 0, 0, 0, ZoneOffset.ofHoursMinutes(5, 30)); EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); NanoPrecisionEntity np = new NanoPrecisionEntity(); np.setPlainLocalDateTime(localDateTime); em.persist(np); TimeZoneStorageEntity tz = new TimeZoneStorageEntity(); tz.setNativeMode(explicitOffset); em.persist(tz); em.getTransaction().commit(); Long npId = np.getId(); Long tzId = tz.getId(); em.close(); // Read back the raw stored value via native SQL to see the literal DB representation. EntityManager em2 = emf.createEntityManager(); Object rawLocalDateTime = em2.createNativeQuery( "SELECT CAST(plain_local_date_time AS VARCHAR) FROM nano_precision WHERE id = :id") .setParameter("id", npId).getSingleResult(); Object rawOffset = em2.createNativeQuery( "SELECT CAST(native_col AS VARCHAR) FROM tz_storage WHERE id = :id") .setParameter("id", tzId).getSingleResult(); NanoPrecisionEntity npLoaded = em2.find(NanoPrecisionEntity.class, npId); TimeZoneStorageEntity tzLoaded = em2.find(TimeZoneStorageEntity.class, tzId); DEMO.info("hibernate.jdbc.time_zone=America/New_York -- original LocalDateTime = {}", localDateTime); DEMO.info("hibernate.jdbc.time_zone=America/New_York -- raw DB value for LocalDateTime column = {}", rawLocalDateTime); DEMO.info("hibernate.jdbc.time_zone=America/New_York -- round-tripped LocalDateTime = {}", npLoaded.getPlainLocalDateTime()); DEMO.info("hibernate.jdbc.time_zone=America/New_York -- original OffsetDateTime (NATIVE) = {}", explicitOffset); DEMO.info("hibernate.jdbc.time_zone=America/New_York -- raw DB value for NATIVE offset column = {}", rawOffset); DEMO.info("hibernate.jdbc.time_zone=America/New_York -- round-tripped OffsetDateTime (NATIVE) = {}", tzLoaded.getNativeMode()); em2.close(); } }