错误是:发生意外错误(类型=内部服务器错误,状态= 500)。指定的ID不能为null!嵌套异常是java.lang.IllegalArgumentException:给定的id不能为null!org.springframework.dao.InvalidDataAccessApiUsageException
那是我的控制器
@GetMapping("delete/{topic}")
public String delete(Long id) {
topicRepos.deleteById(id);
return "redirect:/topic-list";
}
存储库
public interface TopicRepos extends JpaRepository<Topic,Long> {
public Topic deleteById(int id);}
实体
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
privatre Long id;
private String topicName;
private String topicMessage;
private String topicDescribe;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name="id_topic")
private List<Message> messages;
public Topic() {}
public Topic(String topicName, String topicMessage) {
this.topicName = topicName;
this.topicMessage = topicMessage;
}
public Topic(String topicName, String topicMessage, String topicDescribe) {
this.topicName = topicName;
this.topicMessage = topicMessage;
this.topicDescribe = topicDescribe;
}
public List<Message> getMessages() {
return messages;
}
public void setMessages(List<Message> messages) {
this.messages = messages;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTopicName() {
return topicName;
}
public void setTopicName(String topicName) {
this.topicName = topicName;
}
public String getTopicMessage() {
return topicMessage;
}
public void setTopicMessage(String topicMessage) {
this.topicMessage = topicMessage;
}
public String getTopicDescribe() {
return topicDescribe;
}
public void setTopicDescribe(String topicDescribe) {
this.topicDescribe = topicDescribe;
}
>
html和自由标记代码
<#list topics.content as tempTopic>
<tr>
<td>${tempTopic.topicName}</td>
<td><a href="/topic/${tempTopic.id}" class="btn btn-info btn-sm mb-3">Follow</a>
<a href="/topic/delete/${tempTopic.id}"
class="btn btn-danger btn-sm"
onclick="if (!(confirm('Are you sure you want to delete this topic?'))) return false">
Delete</a></td>
</td>
</tr>
</#list>
您的主题ID未映射到delete
方法的参数。
使用@PathVariable
将路径变量映射到方法参数中。
@GetMapping("delete/{topic}")
public String delete(@PathVariable("topic") Long id) {