I need to store YearMonth
in postgres. According to this SO answer the best postgres type is date
.
Given that we use kotlin + spring-data-r2dbc, how should we implement the mapping?
With an AttributeConverter
I would implement it like this:
import java.time.LocalDate
import java.time.YearMonth
import javax.persistence.AttributeConverter
class YearMonthIntegerAttributeConverter : AttributeConverter<YearMonth?, Int?> {
fun convertToDatabaseColumn(
attribute: YearMonth?
): LocalDate? {
return attribute?.atDay(1)
}
fun convertToEntityAttribute(
date: LocalDate?
): YearMonth? {
if (date == null) {
return null
}
return YearMonth.of(date.year, date.month)
}
}
But using a AttributeConverter
is not possible because javax.persistence
is not available in this stack.