Spring Securityのアカウントと紐づけした個人アカウントの編集機能を作成したいのですが、
submit後にエラーが発生します。Repositoryのsaveメソッドが上手く機能していないと推測しているのですが
どうしたらいいのか、、、
ご教授お願い致します。
開発環境
Java 8
org.springframework.boot <version> 2.2.12.RELEASE
MariaDB <version>10.4.17
errorCode
1There was an unexpected error (type=Internal Server Error, status=500). 2Target object must not be null; nested exception is java.lang.IllegalArgumentException: Target object must not be null 3org.springframework.dao.InvalidDataAccessApiUsageException: Target object must not be null; nested exception is java.lang.IllegalArgumentException: Target object must not be null 4 at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:374) 5 at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:257) 6 at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:531) 7 at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61) 8 at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242) 9 at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:154) 10 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:178) 11 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) 12 at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:95) 13 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) 14 at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) 15 at com.sun.proxy.$Proxy135.save(Unknown Source) 16 at jp.kuroda.sampleBlog.service.PersonService.updatePerson(PersonService.java:29) 17 at jp.kuroda.sampleBlog.controller.PersonController.edit(PersonController.java:51) 18 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 19 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64) 20 at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 21 at java.base/java.lang.reflect.Method.invoke(Method.java:564) 22 at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) 23 at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
Person
1package jp.kuroda.sampleBlog.model; 2 3import java.time.LocalDate; 4import java.util.List; 5 6import javax.persistence.Column; 7import javax.persistence.Entity; 8import javax.persistence.GeneratedValue; 9import javax.persistence.GenerationType; 10import javax.persistence.Id; 11import javax.persistence.OneToMany; 12import javax.validation.constraints.NotBlank; 13import javax.validation.constraints.NotNull; 14import javax.validation.constraints.Size; 15 16import org.springframework.format.annotation.DateTimeFormat; 17 18import lombok.Data; 19 20@Entity 21@Data 22public class Person { 23 @Id 24 @GeneratedValue(strategy=GenerationType.AUTO) 25 private Integer id; 26 27 @NotBlank(message="ユーザー名を入力してください") 28 @Size(max=80,message="ユーザー名は80字以内で入力してください") 29 @Column(length=80) 30 private String name; 31 32 @NotNull(message="誕生日を入力してください") 33 @DateTimeFormat(pattern="yyyy-MM-dd") 34 private LocalDate birthday; 35 36 @OneToMany(mappedBy="person") 37 private List<Blog> blogs; 38}
PersonService
1package jp.kuroda.sampleBlog.service; 2 3import java.time.LocalDate; 4import java.time.LocalDateTime; 5 6import org.springframework.beans.factory.annotation.Autowired; 7import org.springframework.stereotype.Service; 8 9import jp.kuroda.sampleBlog.model.Person; 10import jp.kuroda.sampleBlog.repository.PersonRepository; 11 12@Service 13public class PersonService { 14 @Autowired 15 private PersonRepository personRepository; 16 17 public Person createPerson() { 18 Person person=new Person(); 19 person.setName("名前を設定してください"); 20 person.setBirthday(LocalDate.of(2021, 1, 1)); 21 personRepository.save(person); 22 return person; 23 } 24 public void updatePerson(Person person) { 25 personRepository.save(person); 26 } 27}
UserAccount
1package jp.kuroda.sampleBlog.model; 2 3import javax.persistence.Column; 4import javax.persistence.Entity; 5import javax.persistence.Id; 6import javax.persistence.OneToOne; 7 8import lombok.Data; 9 10@Entity 11@Data 12 13public class UserAccount { 14 @Id 15 @Column(length=80) 16 private String username; 17 18 @Column(length=80) 19 private String password; 20 21 private String type; 22 23 @OneToOne 24 private Person person; 25} 26
UserService
1package jp.kuroda.sampleBlog.service; 2 3import javax.transaction.Transactional; 4 5import org.springframework.beans.factory.annotation.Autowired; 6import org.springframework.context.annotation.Bean; 7import org.springframework.security.core.userdetails.User; 8import org.springframework.security.core.userdetails.UserDetails; 9import org.springframework.security.core.userdetails.User.UserBuilder; 10import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 11import org.springframework.security.crypto.password.PasswordEncoder; 12import org.springframework.security.provisioning.UserDetailsManager; 13import org.springframework.stereotype.Service; 14 15import jp.kuroda.sampleBlog.model.Person; 16import jp.kuroda.sampleBlog.model.UserAccount; 17import jp.kuroda.sampleBlog.repository.UserAccountRepository; 18 19@Service 20public class UserService { 21 @Autowired 22 private UserDetailsManager userDetailsManager; 23 @Autowired 24 private PasswordEncoder passwordEncoder; 25 @Autowired 26 private UserAccountRepository accountRepository; 27 @Autowired 28 private PersonService personService; 29 30 @Bean 31 PasswordEncoder passwordEncoder() { 32 return new BCryptPasswordEncoder(); 33 } 34 public UserAccount find(String username) { 35 return accountRepository.findById(username).get(); 36 } 37 38 @Transactional 39 public UserAccount createUserAccount(String username,String password,String role) { 40 UserBuilder builder=User.builder(); 41 UserDetails userDetails=builder.username(username) 42 .password(passwordEncoder.encode(password)) 43 .roles(role) 44 .build(); 45 userDetailsManager.createUser(userDetails); 46 Person person=personService.createPerson(); 47 UserAccount account=new UserAccount(); 48 account.setUsername(username); 49 account.setPassword(passwordEncoder.encode(password)); 50 account.setType("user"); 51 account.setPerson(person); 52 accountRepository.save(account); 53 return account; 54 } 55}
PersonController
1package jp.kuroda.sampleBlog.controller; 2 3import java.util.List; 4 5import javax.validation.Valid; 6 7import org.springframework.beans.factory.annotation.Autowired; 8import org.springframework.stereotype.Controller; 9import org.springframework.ui.Model; 10import org.springframework.validation.BindingResult; 11import org.springframework.web.bind.annotation.GetMapping; 12import org.springframework.web.bind.annotation.ModelAttribute; 13import org.springframework.web.bind.annotation.PostMapping; 14import org.springframework.web.bind.annotation.RequestMapping; 15 16import jp.kuroda.sampleBlog.model.Blog; 17import jp.kuroda.sampleBlog.model.Person; 18import jp.kuroda.sampleBlog.model.UserAccount; 19import jp.kuroda.sampleBlog.service.BlogService; 20import jp.kuroda.sampleBlog.service.PersonService; 21 22@Controller 23@RequestMapping("/person") 24public class PersonController { 25 @Autowired 26 private PersonService personService; 27 @Autowired 28 private BlogService blogService; 29 30 @ModelAttribute("person") 31 public Person currentPerson(UserAccount userAccount) { 32 return userAccount.getPerson(); 33 } 34 @GetMapping("/index") 35 public String index(Model model) { 36 List<Blog> blogs=blogService.getBlogList(); 37 model.addAttribute("blogs",blogs); 38 return "person/index"; 39 } 40 @GetMapping("/edit") 41 public String edit(Person person) { 42 return"person/form"; 43 } 44 @PostMapping("/edit") 45 public String edit(@Valid Person person,BindingResult bindingResult) { 46 if(bindingResult.hasErrors()) { 47 return"person/form"; 48 } 49 personService.updatePerson(person); 50 return"redirect:/person/index"; 51 }
html
1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org/" 3 xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" 4 xmlns:sec="http://www.thymeleaf.org/extras/spring-security"> 5<head> 6<meta charset="UTF-8"> 7<title>プロフィール編集</title> 8</head> 9<body> 10<form th:action="@{/person/edit}" th:object="${person}" method="post"> 11 <div> 12 名前<br> 13 <input type="text" th:field="*{name}"> 14 </div> 15 <div> 16 誕生日<br> 17 <input type="date" th:field="*{birthday}"> 18 </div> 19 <div> 20 <input type="submit" value="編集"> 21 </div> 22</form> 23</body> 24</html>
PersonRepository
1package jp.kuroda.sampleBlog.repository; 2 3import org.springframework.data.jpa.repository.JpaRepository; 4 5import jp.kuroda.sampleBlog.model.Person; 6 7public interface PersonRepository extends JpaRepository<Person, Integer>{ 8 9} 10コード

回答4件
あなたの回答
tips
プレビュー