1

Been trying to use Micronaut Data module for db persistence but getting following error when I try to save an entity with ZonedDateTime field.

io.micronaut.data.exceptions.DataAccessException: Cannot convert type [class java.time.ZonedDateTime] to target type: class java.util.Date. Consider defining a TypeConverter bean to handle this case.
    at io.micronaut.data.runtime.mapper.QueryStatement.lambda$convertRequired$0(QueryStatement.java:204)
    at java.base/java.util.Optional.orElseThrow(Optional.java:408)
    at io.micronaut.data.runtime.mapper.QueryStatement.convertRequired(QueryStatement.java:203)

I would have assumed that Micronaut would have default implementation to handle Java8 date types. Not sure why it doesn't.

I have also registered a type converter below (where TypeUtils.toUtilDate is internal class which does the actual conversion). I see that Micronaut picked up this class (so it prints a log stmt if I add one).

@Factory
class TypeConverters {

  @Singleton
  fun zdatetimeToUtilDate(): TypeConverter<ZonedDateTime, Date> {
    return TypeConverter { datetime, targetType, context -> Optional.of(TypeUtils.toUtilDate(datetime)) }
  }

}

Even then, I'm still getting the above mentioned error. Any idea why would it be so?

metasync
  • 338
  • 1
  • 10
  • 1
    can you provide an example in an issue report https://github.com/micronaut-projects/micronaut-data/issues – Graeme Rocher Feb 05 '20 at 08:31
  • @GraemeRocher created this bare bone repo to simulate error. https://github.com/brsanthu/mn-test-zdt While creating it, observed that with type converter in place, it is converting and storing the entity. But without type converter it is not (may be it is expected). Will check in our main app why typeconverter is not working, and then will create an issue if required. – metasync Feb 05 '20 at 17:47
  • @GraemeRocher after doing clean compile, TypeConverter approach is working. However, I think that micronaut should have out of the box support without needing another type converter for all java 8 date types. Created a feature request here [https://github.com/micronaut-projects/micronaut-data/issues/394] – metasync Feb 05 '20 at 21:29

0 Answers0