Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
LocalDate format in fields stored as json via Hibernate
Setup
I have a database table with field of JSONB
type.
I access said database using Hibernate
.
In hibernate I have entity mapped on said table.
Entity contains field marked with @JdbcTypeCode(SqlTypes.JSON)
annotation. It have type of some DTO.
In that DTO I have a field of type LocalDate
.
Problem
When I store my entity LocalDate
serialized to json as array [2000, 1, 1]
. I want to have it as string 2000-01-01
.
How can I configure json serialization Hibernate uses?
Code
@Entity
public class E {
@Id
int id;
@JdbcTypeCode(SqlTypes.JSON)
Dto jsonValue;
}
public class Dto {
LocalDate myDate;
}
1 answer
The following users marked this post as Works for me:
User | Comment | Date |
---|---|---|
talex | (no comment) | Oct 24, 2024 at 17:19 |
It is possible to set custom JSON serializer for hibernate.
Here is example:
@Configuration
public class JsonHibernatePropertiesCustomizerConfig {
@Bean
HibernatePropertiesCustomizer customizer(ObjectMapper objectMapper) {
return map -> map.put(AvailableSettings.JSON_FORMAT_MAPPER,
new JacksonJsonFormatMapper(objectMapper));
}
}
0 comment threads