So, my problem is in the title - crudrepository method findAll()
returns a null list. I've overriden Crud's method because it returned an iterable instead of a list.
I've tried using the JpaRepository
instead of CrudRepository
(I didn't override the findAll()
method then), but got the same result.
My code:
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>
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).
When viewing the H2 database console and using the SELECT * FROM CITY
I'm getting the list of the cities.
I am getting a NullPointerException
on this line from the EventController class: model.addAttribute("cities", cityRepository.findAll());
Can someone explain why?
removed the static
declaration in front of the repository, it works fine now