0

I'm trying to retrieve a Date format field in a MongoDB collection from a Spring Boot Application. In the Repository I've written the native MongoDB query for the method. When I run the application, I get the errors like:

**org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bookingHistoryController': Unsatisfied dependency expressed through field 'bookingHistoryService': Error creating bean with name 'bookingHistoryService': Unsatisfied dependency expressed through field 'bookingHistoryRepo': Error 
creating bean with name 'bookingHistoryRepo' defined in 
hotelbookingsystem.repositories.BookingHistoryRepo defined in 
@EnableMongoRepositories declared on 
MongoRepositoriesRegistrar.EnableMongoRepositoriesConfiguration: 
Could not create query for public abstract java.util.List 
otelbookingsystem.repositories.BookingHistoryRepo.findCheckinByCustomerId(java.lang.Integer); Reason: 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 @77523395**

And,

**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 @77523395**

I understand that the issue has something to do with --add-opens java.base/java.time=ALL-UNNAMED but guide me a better way to implement this in IntelliJ.

Java class for the 'bookinghistories' collection in MongoDB.

package hotelbookingsystem.documents;

import java.time.LocalDateTime;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
import org.springframework.format.annotation.DateTimeFormat;

@Document(collection = "bookinghistories")
public class BookingHistory {

    @Id
    @Field(name = "_id")
    private String id;

    @Field(name = "customer_id")
    private Integer customerId;

    @Field(name = "room_number")
    private Integer roomNumber;

    @Field(name = "checkin_date")
    private LocalDateTime checkinDate;

    @Field(name = "checkout_date")
    private LocalDateTime checkoutDate;

    @Field(name = "days_of_stay")
    private Integer daysOfStay;

    public BookingHistory(String id, Integer customerId, Integer roomNumber, LocalDateTime checkinDate,
                          LocalDateTime checkoutDate, Integer daysOfStay) {
        super();
        this.id = id;
        this.customerId = customerId;
        this.roomNumber = roomNumber;
        this.checkinDate = checkinDate;
        this.checkoutDate = checkoutDate;
        this.daysOfStay = daysOfStay;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Integer getCustomerId() {
        return customerId;
    }

    public void setCustomerId(Integer customerId) {
        this.customerId = customerId;
    }

    public Integer getRoomNumber() {
        return roomNumber;
    }

    public void setRoomNumber(Integer roomNumber) {
        this.roomNumber = roomNumber;
    }

    public LocalDateTime getCheckinDate() {
        return checkinDate;
    }

    public void setCheckinDate(LocalDateTime checkinDate) {
        this.checkinDate = checkinDate;
    }

    public LocalDateTime getCheckoutDate() {
        return checkoutDate;
    }

    public void setCheckoutDate(LocalDateTime checkoutDate) {
        this.checkoutDate = checkoutDate;
    }

    public Integer getDaysOfStay() {
        return daysOfStay;
    }

    public void setDaysOfStay(Integer daysOfStay) {
        this.daysOfStay = daysOfStay;
    }
}

The Repository:

package hotelbookingsystem.repositories;


import hotelbookingsystem.documents.BookingHistory;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;

import java.time.LocalDateTime;
import java.util.List;

public interface BookingHistoryRepo extends MongoRepository<BookingHistory, Integer> {

    @Query(value = "{customer_id : ?0}", fields = "{_id: 0, checkin_date: 1}")
    List<LocalDateTime> findCheckinByCustomerId(Integer customerId);

}

The program is expected to retrieve the list of LocalDateTime Objects for the 'checkin_date' field in MongoDB for the given 'customer_id'.

This is how the collection looks: Screenshot from MongoDB Compass of the collection

  • 1
    By the way, you have omitted the "Time" part of a `LocalDateTime` in your `@DateTimeFormat(pattern = "dd/MM/yyyy")` code. – Basil Bourque Apr 02 '23 at 06:40
  • Thank you for the detail. It was actually an unedited detail from previous code. It was not planned to be put there. – Govardhan Lingampalli Apr 02 '23 at 17:09
  • 2
    Does this answer your question? [JDK 17 spring boot Unable to make private java.time.LocalDateTime](https://stackoverflow.com/questions/69352207/jdk-17-spring-boot-unable-to-make-private-java-time-localdatetime) – David Jones Apr 02 '23 at 17:19

0 Answers0