ブログ作成アプリを作成中です。
投稿した記事を編集する機能を実装中に下記のエラーが発生しました。
A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: jp.kuroda.sampleBlog.model.Blog.comments; nested exception is org.hibernate.HibernateException: at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) at com.sun.proxy.$Proxy1467.save(Unknown Source) at jp.kuroda.sampleBlog.service.BlogService.editBlog(BlogService.java:54) at jp.kuroda.sampleBlog.controller.PersonController.editBlog(PersonController.java:100)
当該コードは下記になります。
Java
1@Entity 2@Data 3public class Blog { 4 @ManyToOne 5 private Person person; 6 @OneToMany(mappedBy="blog",cascade=CascadeType.ALL,orphanRemoval=true) 7 private List<Comment> comments; 8}
Java
1@Entity 2@Data 3public class Comment { 4 @ManyToOne 5 private Blog blog; 6 @ManyToOne 7 private Person person; 8}
Java
1@Service 2public class BlogService { 3 @Autowired 4 private BlogRepository blogRepository; 5 6 public List<Blog> getBlogList(){ 7 return blogRepository.findAll(); 8 } 9 public List<Blog> getMyBlogs(Person person){ 10 return blogRepository.findByPerson(person); 11 } 12 13 public Blog createBlog(Blog blog){ 14 blog.setPostDateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"))); 15 if(!blog.getFile().isEmpty()) { 16 try { 17 MultipartFile file=blog.getFile(); 18 String fileName=StringUtils.cleanPath(file.getOriginalFilename()); 19 blog.setImage_name(fileName); 20 blog.setImage_byte(file.getBytes()); 21 StringBuffer data = new StringBuffer(); 22 String base64 = new String(Base64.encodeBase64(file.getBytes()),"ASCII"); //画像をbase64に置き換えて文字列化 23 data.append("data:image/jpeg;base64,"); 24 data.append(base64); 25 blog.setBase64_str(data.toString()); 26 }catch(Exception e) { 27 blog.setImage_name(null); 28 } 29 } 30 blogRepository.save(blog); 31 return blog; 32 } 33 public void editBlog(Blog blog) { 34 blog.setPostDateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"))); 35 36 blogRepository.save(blog); 37 } 38 public void deleteBlog(Blog blog) { 39 blogRepository.deleteById(blog.getId()); 40 }
Java
1@Service 2public class PersonService { 3 @Autowired 4 private PersonRepository personRepository; 5 6 @Autowired 7 private CommentRepository commentRepository; 8 public Person createPerson() { 9 Person person=new Person(); 10 person.setName("名前を設定してください"); 11 person.setBirthday(LocalDate.of(2021, 1, 1)); 12 personRepository.save(person); 13 return person; 14 } 15 public Comment getComment(Blog blog,Person person) { 16 Comment comment=new Comment(); 17 comment.setBlog(blog); 18 comment.setPerson(person); 19 return comment; 20 } 21 public Comment createComment(Comment comment) { 22 comment.setPostDateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"))); 23 commentRepository.save(comment); 24 return comment; 25 } 26 public void deleteComment(Comment comment) { 27 commentRepository.deleteById(comment.getId()); 28 } 29}
Java
1@Controller 2@RequestMapping("/person") 3public class PersonController { 4 @Autowired 5 private PersonService personService; 6 @Autowired 7 private BlogService blogService; 8@GetMapping("/blog/{blogId}/edit") 9 public String editBlog(@PathVariable("blogId")Blog blog,Model model) { 10 model.addAttribute("base64image",blog.getBase64_str()); 11 model.addAttribute("blog",blog); 12 return"person/create"; 13 } 14 @PostMapping("/blog/{blogId}/edit") 15 public String editBlog(@Valid Blog blog,BindingResult bindingResult) { 16 if(bindingResult.hasErrors()) { 17 return"person/create"; 18 } 19 blogService.editBlog(blog); 20 return"redirect:/person/edit"; 21 } 22}
上記のリンク先の記事を参考に、blogServiceクラスのeditBlogメソッドに、
blog.getComments().clear();
blog.getComments().addAll(blog.getCommetns());
をダメもとで追記しましたがやはりえらーは解消されず、、、
blogクラスのmappedBy以降は、記事投稿者がブログ記事を削除するために必要なコードになります。
記事編集機能を追加するためにどうしたらいいのか教えてください。
開発環境
java 8
spring framework.boot version2.2.12
10.4.17-MariaDB -
あなたの回答
tips
プレビュー