【问题标题】:Spring CrudRepository method findAll() returns an empty listSpring CrudRepository 方法 findAll() 返回一个空列表
【发布时间】:2019-05-27 21:04:58
【问题描述】:

所以,我的问题在于标题 - crudrepository 方法 findAll() 返回一个空列表。我已经覆盖了 Crud 的方法,因为它返回了一个可迭代的而不是一个列表。

我尝试使用JpaRepository 代替CrudRepository(当时我没有覆盖findAll() 方法),但得到了相同的结果。

我的代码:

Event.java

@Data
@Entity
@NoArgsConstructor
public class Event{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @Column(nullable=false)
    private String name;
    
    @Column(nullable=false)
    private LocalDateTime time;

    @ManyToOne
    @JoinColumn
    private City city;

}

City.java

@Data
@Entity
@NoArgsConstructor
public class City {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @Column(nullable=false)
    private Long code;
    
    @Column(nullable=false)
    private String name;
    
        @OneToMany(mappedBy = "city", cascade = CascadeType.ALL)
        private Set<Event> events;
    }

CityRepository.java

    @Repository
    public interface CityRepository extends CrudRepository<City, Long> {
    
    @Override
    List<City> findAll();
}

EventsController.java

@Controller
@RequestMapping
public class EventsController {

    @Autowired
    static CityRepository cityRepository;

    public static List<Event> eventList = new ArrayList<>();
    
    @RequestMapping
    public String eventEntry(Model model) {
        
        model.addAttribute("event", new Event());
        model.addAttribute("cities", cityRepository.findAll());
        
        return "eventEntry";
    }
}

eventEntry.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Event entry</title>
</head>
<body>

    <h3>New event</h3>
    
    <form method="POST" th:object="${event}">
    
        <div class="form-group">
            <label for="city">City: </label>
            <select th:field="*{city}">
                <option value="" >Choose a city</option>
                <option th:each="city : ${cities}" th:value="${city}" th:text="${city}"></option>
            </select>
            <span class="validation-error" th:if="${#fields.hasErrors('city')}" th:errors="*{city}">City Error</span>
        </div>
    
        <div class="form-group">
            <label for="name">Name: </label>
            <input type="text" th:field="*{name}" />
            <span class="validation-error" th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Name Error</span>
        </div>
        
        <div class="form-group">
            <label for="time">Time: </label>
            <input type="datetime-local" th:field="*{time}" />
            <span class="validation-error" th:if="${#fields.hasErrors('time')}" th:errors="*{time}">Time Error</span>
        </div>
                    
        <div class="form-group">
            <input type="submit" th:value="Save">
        </div>
        
    </form>
</body>

我正在使用带有 data.sql 文件的嵌入式 H2 数据库,用于将数据填充到数据库中(我没有 schema.sql 文件,因为表是使用注释创建的)。 查看 H2 数据库控制台并使用 SELECT * FROM CITY 时,我正在获取城市列表。

我在 EventController 类的这条线上得到了一个NullPointerExceptionmodel.addAttribute("cities", cityRepository.findAll()); 谁能解释一下原因?

【问题讨论】:

  • findAll() 不返回空列表。你得到一个 NPE 因为cityRepository 是空的。 Spring 永远不会自动装配静态字段。不要做假设。使用您的调试器或代码中的跟踪来了解正在发生的事情。
  • 多么疏忽……我只是删除了存储库前面的static 声明,它现在可以工作了……我以后会更加努力地研究我自己的代码

标签: java spring spring-repositories


【解决方案1】:

删除了存储库前面的static 声明,现在可以正常工作了

【讨论】:

    猜你喜欢
    • 2018-06-01
    • 2021-05-20
    • 2021-09-03
    • 1970-01-01
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多