Me and my colleagues noticed weird behaviour on our local environments (Manjaro & Windows) thanks to integration tests.
Field of the type OffsetDateTime differs in nano before and after real save to the DB. I was trying to find some information and it seems, that there is some time truncation or rounding. But weird thing is that this is happening on some of our locals environments, but not in our pipelines (GitLab).
Simple test code:
val plainEntity = Entity(url = "url")
val savedEntity = repository.save(plainEntity)
val retriEntity = repository.findByUrl(plainEntity.url)
assertTrue(savedEntity.date.isEqual(retriEntity?.date)) // this is false due difference in nano part
Produces such data:
plainEntity.date //2022-08-18T13:35:36.953334471+02:00
savedEntity.date //2022-08-18T13:35:36.953334471+02:00
retriEntity.date //2022-08-18T13:35:36.953334+02:00
Entity (simplified):
@Entity
@Table(name = TABLE_NAME)
@SequenceGenerator(name = SEQUENCE_GEN, sequenceName = SEQUENCE_NAME, allocationSize = 1)
class Entity(
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = SEQUENCE_GEN)
var id: Long = 0L,
@Column(nullable = false)
val url: String,
@Column(nullable = false)
var date: OffsetDateTime = OffsetDateTime.now()
// there is no difference if I remove this default value and set it in construction of the entity
)
Repository:
@Repository
interface Repository : CrudRepository<Entity, Long> {
fun findByUrl(url: String): Entity?
}
Table creation:
<createTable tableName="tableName">
<column name="id" type="bigint" defaultValueSequenceNext="table_id_seq">
<constraints primaryKey="true" primaryKeyName="table_id_pk" nullable="false"/>
</column>
<column name="url" type="text">
<constraints nullable="false" unique="true"/>
</column>
<column name="date" type="timestamp with time zone">
<constraints nullable="false"/>
</column>
</createTable>
Project set-up:
- spring-boot app
- spring data JPA
- Liquibase for definitions
- testcontainers with Postgres in tests