[英]Spring CrudRepository method findAll() returns an empty list
So, my problem is in the title - crudrepository method findAll()
returns a null list. 所以,我的问题在于标题 - crudrepository方法
findAll()
返回一个空列表。 I've overriden Crud's method because it returned an iterable instead of a list. 我已经覆盖了Crud的方法,因为它返回了一个iterable而不是一个列表。
I've tried using the JpaRepository
instead of CrudRepository
(I didn't override the findAll()
method then), but got the same result. 我已经尝试使用
JpaRepository
而不是CrudRepository
(我没有覆盖findAll()
方法),但得到了相同的结果。
My code: 我的代码:
Event.java 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 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 CityRepository.java
@Repository
public interface CityRepository extends CrudRepository<City, Long> {
@Override
List<City> findAll();
}
EventsController.java 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 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>
I am using an embedded H2 database with a data.sql
file used to fill data into the database (I don't have the schema.sql
file since tables are created using annotations). 我使用嵌入式H2数据库和
data.sql
文件,用于将数据填充到数据库中(我没有schema.sql
文件,因为表是使用注释创建的)。 When viewing the H2 database console and using the SELECT * FROM CITY
I'm getting the list of the cities. 查看H2数据库控制台并使用
SELECT * FROM CITY
我将获取城市列表。
I am getting a NullPointerException
on this line from the EventController class: model.addAttribute("cities", cityRepository.findAll());
我从EventController类获取此行的
NullPointerException
: model.addAttribute("cities", cityRepository.findAll());
Can someone explain why? 有人可以解释原因吗?
删除了存储库前面的static
声明,现在工作正常
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.