When using Datetime arithmetic to add 1 hour to an Instant attribute the result is shifted by 2 hours (Europe/Berlin DST):
select e.instant + 0 hour from DummyEntity e
In the database table the column is of type timestamp with time zone. We do not set hibernate.jdbc.time_zone. Since Instant and timestamp with time zone are about UTC this should be not necessary?
Full example:
@Entity
@Table(name = "dummy")
public class DummyEntity {
@Id
@Column(name = "id")
private int id;
@Column(name = "instant")
private Instant instant;
public DummyEntity() {
}
public DummyEntity(int id, Instant instant) {
this.id = id;
this.instant = instant;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Instant getInstant() {
return instant;
}
public void setInstant(Instant instant) {
this.instant = instant;
}
}
static void main() {
TimeZone.setDefault(TimeZone.getTimeZone("Europe/Berlin"));
try (SessionFactory sessionFactory = new Configuration().configure()
.buildSessionFactory(); Session session = sessionFactory.openSession()) {
session.beginTransaction();
session.createNativeQuery("""
set TIMEZONE = 'Europe/Berlin';
""").executeUpdate();
session.createNativeQuery("""
create table dummy (
id integer primary key,
instant timestamp with time zone
)
""").executeUpdate();
DummyEntity newEntity = new DummyEntity(999, Instant.now());
session.persist(newEntity);
Instant result1 = session.createQuery("select e.instant + 0 hour from DummyEntity e", Instant.class)
.list()
.getFirst();
// result is shifted by 2 hours
IO.println(result1);
Instant result2 = session.createQuery("select e.instant from DummyEntity e", Instant.class).list().getFirst();
// result is ok
IO.println(result2);
Instant result3 = session.createQuery("select cast(e.instant + 0 hour as Instant) from DummyEntity e", Instant.class)
.list()
.getFirst();
// result is ok
IO.println(result3);
session.getTransaction().rollback();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"https://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/mydb</property>
<property name="hibernate.connection.username">myuser</property>
<property name="hibernate.connection.password">mypassword</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<mapping class="xxx.DummyEntity"/>
</session-factory>
</hibernate-configuration>
Hibernate: 6.6.47.Final
Postgres driver: 42.7.10
Postgres: 18.3
Java: OpenJDK 25.0.2
Is this a limitation or a bug of the Datetime arithmetic in Hibernate? Are we doing something wrong?