0

I'm using Spring boot with mongodb. I used LocalDateTime type for createdAt key. This is sample code to explain my situation.

@Data
@NoArgsConstructor
@Document("User")
public class User {

  @Id
  private ObjectId id;

  @CreatedDate
  private LocalDateTime createdAt;

  @LastModifiedDate
  private LocalDateTime updatedAt;

  private String username;

Then, spring boot throw following error.

Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make private java.time.LocalDateTime(java.time.LocalDate,java.time.LocalTime) accessible: module java.base does not "opens java.time" to unnamed module

Why this error is occured, and how can I fix it?

I'm using java-17-amazon-corretto. If you need more information, I'll add it.

  • Does this answer your question? https://stackoverflow.com/questions/70412805/what-does-this-error-mean-java-lang-reflect-inaccessibleobjectexception-unable – JustAnotherDeveloper Jul 07 '22 at 15:32
  • That answer fixed the error when run directly. But when I try to test, I got same error. btw, it seems like I found another way to fix it from that answer. During my application is running, there is following message in log. `WARN 17980 --- [ restartedMain] o.s.data.convert.CustomConversions : Registering converter from class java.time.LocalDateTime to class org.joda.time.LocalDateTime as reading converter although it doesn't convert from a store-supported type; You might want to check your annotation setup at the converter implementation` – GrassHopper42 Jul 07 '22 at 16:00
  • Application tried to convert `java.time.LocalDateTime` to `org.joda.time.LocalDateTime`, so I change import package from `java.time.LocalDateTime` to `org.joda.time.LocalDateTime`. And the error has been fixed! – GrassHopper42 Jul 07 '22 at 16:01
  • By the way, `LocalDateTime` is the wrong type to record a moment, a specific point on the timeline. Use `Instant` instead. Search to learn more. I and others have written extensively on this. – Basil Bourque Jul 07 '22 at 19:25

0 Answers0