SpringBootにおいて、該当のページへpostリクエストを送りましたら、エラーが発生してしまいました。
以下、MsgDataController.java
です
package com.tuyano.springboot; import java.util.List; import javax.annotation.PostConstruct; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.Errors; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import com.tuyano.springboot.repositories.MsgDataRepository; @Controller public class MsgDataController{ @Autowired MsgDataRepository repository; @PersistenceContext EntityManager entityManager; MsgDataDaoImpl dao; @RequestMapping(value = "/msg",method = RequestMethod.GET) public ModelAndView msg(ModelAndView mav) { mav.setViewName("showMsgData"); mav.addObject("title","Sample"); mav.addObject("msg","MsgDataのサンプルです"); MsgData msgdata = new MsgData(); mav.addObject("formModel",msgdata); List<MsgData> list = (List<MsgData>)dao.getAll(); mav.addObject("datalist",list); return mav; } @RequestMapping(value = "/msg",method = RequestMethod.POST) @Transactional(readOnly=false) public ModelAndView msgform(@Valid @ModelAttribute MsgData msgdata,Errors result, ModelAndView mav) { if(result.hasErrors()) { mav.setViewName("showMsgData"); mav.addObject("title","Sample [ERROR]"); mav.addObject("msg","値を再チェックしてください!"); return mav; }else { repository.save(msgdata); System.out.println("test"); return new ModelAndView("redirect:/msg"); } } @PostConstruct public void init() { System.out.println("ok"); dao = new MsgDataDaoImpl(entityManager); } }
以下は、showMsgData.htmlです
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title th:text="${title}">top page</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <style> h1{ font-size:18pt; font-weight:bold; color:gray; } body{ font-size:13pt; color:gray; margin:5px 25px;} tr { margin:5px;} th{ padding:5px; color:white; background:darkgray;} td{ padding:5px;color:black;background:#e0e0ff;} </style> </head> <body> <h1 th:text="${title}">MyMsg page</h1> <p th:text="${msg}"> <table> <form method="post" action="/msg" th:object="${formModel}"> <input type="hidden" name="id" th:value="*{id}"> <tr> <td><label for="title">タイトル</lavel></td> <td><input type-"text" name="title" th:value="*{title}"\></td> </tr> <tr> <td><label for="message">メッセージ</label></td> <td><textarea name="message" th:text="*{message}"></textarea></td> </tr> <tr> <td><label for="mydata">MYDATA_ID</label></td> <td><textarea name="mydata}"></textarea></td> </tr> <tr> <td></td> <td><input type="submit"></td> </tr> </form> </table> <hr> <table> <tr><th>ID</th><th>名前</th><th>タイトル</th></tr> <tr th:each="obj : ${datalist}"> <td th:text="${obj.id}"></td>> <td th:text="${obj.mydata.name}"></td> <td th:text="${obj.title}"></td> </tr> </table> </body> </html>
まず、Eclipseの再生ボタンを押し「localhost:8080/msg」へアクセスし、form内に適当な値を入れた後、送信ボタンを押しました。
しかし、遷移先のページでエラーが表示されました。(文字数制限がかかってしまう為、一部抜粋)
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Wed Jun 03 10:51:42 JST 2020
There was an unexpected error (type=Internal Server Error, status=500).
org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : com.tuyano.springboot.MsgData.mydata -> com.tuyano.springboot.MyData; nested exception is java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : com.tuyano.springboot.MsgData.mydata -> com.tuyano.springboot.MyData
org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : com.tuyano.springboot.MsgData.mydata -> com.tuyano.springboot.MyData; nested exception is java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : com.tuyano.springboot.MsgData.mydata -> com.tuyano.springboot.MyData
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:371)
postリクエストを送った際、MsgDataController.java
内のmsgform
メソッド内のSystem.out.println("test");
ここの部分はエラーになる前に出力されている為、恐らくreturn new ModelAndView("redirect:/msg");
ここの部分がエラーの原因ではないかと、推測しています。しかし、ここの何がが問題なのかが分からない状況です。
こちら、エラーの解消をしたい為、ご助言頂けましたら幸いです。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/06/03 08:21 編集
2020/06/03 08:21 編集
2020/06/03 08:32