-1
私は同様の問題を捜し求めましたが、いくつかの関連する質問は見つかりませんでした。だから私はここに私の問題を投稿して、より良い運を望む。ThymeleafとSpring Bootを使用して子どもとレコードを更新できない - NullPointerExceptionを取得する
子ども(1対多)ResourceSkills
の親レコードResource
があります。私は、ResourceSkills
がNullであることを私のコントローラクラスに見ても、私はリソースを編集するために使用するThymeleafページにResource
とResourceSkills
の両方の値を表示できます。この結果、NullPointerExceptionが発生します。だからここ
はコードです:
リソース
@Entity
public class Resource {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Version
private Integer version;
private String firstName;
private String lastName;
@OneToMany (mappedBy = "resource", orphanRemoval=false, fetch=FetchType.EAGER)
private List<ResourcesSkills> skills;
@ManyToOne (fetch=FetchType.EAGER)
@JoinColumn(name="project_id")
private Project project;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public List<ResourcesSkills> getSkills() {
return skills;
}
public void setSkills(List<ResourcesSkills> skills) {
this.skills = skills;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getVersion() {
return version;
}
public void setVersion(Integer version) {
this.version = version;
}
public Project getProject() {
return project;
}
public void setProject(Project project) {
this.project = project;
}
}
ResourcesSkills
@Entity
@IdClass(ResourcesSkillsId.class)
public class ResourcesSkills {
@Id
@ManyToOne
@JoinColumn(name="resource_id")
private Resource resource;
@Id
@ManyToOne
@JoinColumn(name = "skill_id")
private Skill skill;
@Version
private Integer version;
private boolean primarySkill;
public Resource getResource() {
return resource;
}
public void setResource(Resource resource) {
this.resource = resource;
}
public Skill getSkill() {
return skill;
}
public void setSkill(Skill skill) {
this.skill = skill;
}
public Integer getVersion() {
return version;
}
public void setVersion(Integer version) {
this.version = version;
}
public boolean isPrimarySkill() {
return primarySkill;
}
public void setPrimarySkill(boolean primarySkill) {
this.primarySkill = primarySkill;
}
}
ResourceController
@Controller
public class ResourceController {
ResourceService resourceService;
SkillService skillService;
@Autowired
public void setResourceService(ResourceService resourceService){
this.resourceService = resourceService;
}
@Autowired
public void setSkillService(SkillService skillService){
this.skillService = skillService;
}
@RequestMapping(value="resources", method = RequestMethod.GET)
public String list(Model model){
model.addAttribute("resources", resourceService.listAllResources());
return "resources";
}
@RequestMapping(value="resource/{id}")
public String showResource(@PathVariable Integer id, Model model){
model.addAttribute("resource", resourceService.getResourceById(id));
return "resourceshow";
}
@RequestMapping(value = "resource/new")
public String newResource(Model model){
model.addAttribute("resource", new Resource());
model.addAttribute("skills", skillService.listAllSkills());
return "resourceform";
}
@RequestMapping(value = "resource/edit/{id}")
public String editResource(@PathVariable Integer id, Model model){
Resource resource = resourceService.getResourceById(id);
model.addAttribute("resource", resource);
return "resourceform";
}
@RequestMapping(value = "resource/delete/{id}")
public String deleteResource(@PathVariable Integer id){
resourceService.deleteResource(id);
return "redirect:/resources";
}
@RequestMapping(value = "resource", method = RequestMethod.POST)
public String saveResource(Resource resource){
/* AT THIS POINT resource.skills is NULL and save FAILS */
ResourceService.saveResource(resource);
return "redirect:/resource/" + resource.getId();
}
}
そして最後にThymeleafコード:
<h2>Resource Details</h2>
<div>
<form class="form-horizontal" th:object="${resource}" th:action="@{/resource}" method="post">
<input type="hidden" th:field="*{id}"/>
<input type="hidden" th:field="*{version}"/>
<div class="form-group">
<label class="col-sm-2 control-label">First name:</label>
<div class="col-sm-10">
<input type="text" class="form-control" th:field="*{firstName}"/>
</div>
<label class="col-sm-2 control-label">Surname:</label>
<div class="col-sm-10">
<input type="text" class="form-control" th:field="*{lastName}"/>
</div>
</div>
<div>
<table class="table table-stripped">
<tr>
<th>Skill</th>
<th>Primary</th>
</tr>
<tr th:each="resSkill : *{skills}">
<td th:text="${resSkill.skill.description}">Skill Description</td>
<td><input type="checkbox" name="primary" th:disabled="disabled" th:checked="${resSkill.primarySkill}" /></td>
</tr>
</table>
<a th:href="${'/resourceSkills/' + resource.id}">Edit Skills</a>
</div>
<div class="row">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</form>
</div>
私が手にエラーが(参照用)以下の通りです:あなたはResourceServiceと@Autowireを忘れてしまったよう
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException: null
at eu.kororos.ccplanner.controllers.ResourceController.saveResource(ResourceController.java:79) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_65]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_65]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_65]
at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_65]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:222) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
レスポンスありがとうMarcoしかし確かに私はそれを得る...これは私がResourceControllerの最初の2つの方法でやっていることです。私はそれの上に何かをする必要がありますか? – Lefteris