如何修复“无法使私有 java.time.LocalDateTime(java.time.LocalDate,java.time.LocalTime) 可访问”

问题描述 投票:0回答:0

我正在尝试从 Spring Boot 应用程序中检索 MongoDB 集合中的日期格式字段。在存储库中,我为该方法编写了本机 MongoDB 查询。当我运行应用程序时,出现如下错误:

org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“bookingHistoryController”的 bean 时出错:通过字段“bookingHistoryService”表示的依赖项不满足:创建名称为“bookingHistoryService”的 bean 时出错:通过字段“bookingHistoryRepo”表示的依赖项不满足:创建时出错在 hotelbookingsystem.repositories.BookingHistoryRepo 中定义的名为“bookingHistoryRepo”的 bean 在 MongoRepositoriesRegistrar 上声明的@EnableMongoRepositories 中定义。原因:无法使私有 java.time.LocalDateTime(java.time.LocalDate,java.time.LocalTime) 可访问:模块 java.base 不会“打开 java.time”到未命名的模块 @77523395

还有,

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" 到未命名模块@77523395

我知道这个问题与

--add-opens java.base/java.time=ALL-UNNAMED
有关,但指导我更好的方法在 IntelliJ 中实现它。

MongoDB 中“bookinghistories”集合的 Java 类。

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;

    @DateTimeFormat(pattern = "dd/MM/yyyy")
    @Field(name = "checkin_date")
    private LocalDateTime checkinDate;

    @DateTimeFormat(pattern = "dd/MM/yyyy")
    @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;
    }
}

存储库:

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);

}

该程序需要为给定的“customer_id”检索 MongoDB 中“checkin_date”字段的 LocalDateTime 对象列表。

这就是这个系列的样子: Screenshot from MongoDB Compass of the collection

java spring-boot intellij-idea spring-data-mongodb
© www.soinside.com 2019 - 2024. All rights reserved.