spring Boot2とMySQLを用いて複数エンティティの連携を行いたいのですがうまくいきません。
idのプロパティが見つからないということでエラーが出るのですが、原因が分からず止まっています。
thymeleafの問題ではなく、entityがうまく連携できていないのだとは考えています...
ご教示いただけると幸いです。
java
1package com.tuyano.springboot; 2 3import javax.persistence.*; 4import javax.validation.constraints.*; 5 6 7@Entity 8@Table(name="msgdata") 9public class MsgData { 10 11 @Id 12 @GeneratedValue(strategy = GenerationType.AUTO) 13 @Column 14 @NotNull 15 private long id; 16 17 @Column 18 private String title; 19 20 @Column(nullable = false) 21 @NotEmpty 22 private String message; 23 24 @ManyToOne 25 private MyData mydata; 26 27 public MsgData() { 28 super(); 29 mydata = new MyData(); 30 } 31 32 public long getId() { 33 return id; 34 } 35 36 public void setId(long id) { 37 this.id = id; 38 } 39 40 public String getTitle() { 41 return title; 42 } 43 44 public void setTitle(String title) { 45 this.title = title; 46 } 47 48 public String getMessage() { 49 return message; 50 } 51 52 public void setMessage(String message) { 53 this.message = message; 54 } 55 56 public MyData getMydata() { 57 return mydata; 58 } 59 60 public void setMydata(MyData mydata) { 61 this.mydata = mydata; 62 } 63 64} 65
java
1package com.tuyano.springboot; 2 3import javax.persistence.*; 4import java.util.*; 5import javax.validation.constraints.*; 6 7 8@Entity 9@Table(name="mydata") 10public class MyData { 11 12 @OneToMany(cascade=CascadeType.ALL) 13 @Column(nullable = true) 14 private List<MsgData> msgdatas; 15 16 @Id 17 @GeneratedValue(strategy=GenerationType.AUTO) 18 @Column 19 @NotNull 20 private long id; 21 22 @Column(length = 50, nullable=false) 23 @NotEmpty 24 private String name; 25 26 @Column(length = 200, nullable = true) 27 @Email 28 private String mail; 29 30 @Column(nullable = true) 31 @Min(0) 32 @Max(200) 33 private Integer age; 34 35 @Column(nullable=true) 36 private String memo; 37 38 39 40 public long getId() { 41 return id; 42 } 43 44 public void setId(long id) { 45 this.id = id; 46 } 47 48 public String getName() { 49 return name; 50 } 51 52 public void setName(String name) { 53 this.name = name; 54 } 55 56 public String getMail() { 57 return mail; 58 } 59 60 public void setMail(String mail) { 61 this.mail = mail; 62 } 63 64 public Integer getAge() { 65 return age; 66 } 67 68 public void setAge(Integer age) { 69 this.age = age; 70 } 71 72 public String getMemo() { 73 return memo; 74 } 75 76 public void setMemo(String memo) { 77 this.memo = memo; 78 } 79 80 public List<MsgData> getMsgdatas() { 81 return msgdatas; 82 } 83 84 public void setMsgdatas(List<MsgData> msgdatas) { 85 this.msgdatas = msgdatas; 86 } 87 88 89} 90
java
1package com.tuyano.springboot; 2 3import java.io.Serializable; 4import java.util.*; 5 6public interface MsgDataDao<T> { 7 8 public List<MsgData> getAll(); 9 public MsgData findById(long id); 10} 11
java
1package com.tuyano.springboot; 2 3import java.util.*; 4import javax.persistence.*; 5import org.springframework.stereotype.*; 6 7 8@SuppressWarnings("rawtypes") 9@Repository 10public class MsgDataDaoImpl implements MsgDataDao<MsgDataDao>{ 11 12 private EntityManager entityManager; 13 14 public MsgDataDaoImpl() { 15 super(); 16 } 17 public MsgDataDaoImpl(EntityManager manager) { 18 entityManager = manager; 19 } 20 21 @SuppressWarnings("unchecked") 22 @Override 23 public List<MsgData> getAll() { 24 return entityManager 25 .createQuery("from MsgData") 26 .getResultList(); 27 } 28 29 @Override 30 public MsgData findById(long id) { 31 return (MsgData)entityManager 32 .createQuery("from MsgData where id=" + id).getSingleResult(); 33 } 34} 35
java
1package com.tuyano.springboot.repositories; 2 3import org.springframework.data.jpa.repository.JpaRepository; 4import org.springframework.stereotype.*; 5import com.tuyano.springboot.*; 6 7 8@Repository 9public interface MsgDataRepository extends JpaRepository<MsgData,Long> { 10 11} 12
html
1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org"> 3<head> 4<title>Insert title here</title> 5<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 6 7</style> 8</head> 9<body> 10 <h1 th:text="#{content.title}">MyMsg page</h1> 11 <p th:text="${msg}"></p> 12 <table> 13 <form method="post" action="/msg" th:object="${formModel}"> 14 <input type="hidden" name = "id" th:value="*{id}"/> 15 <tr><td><label for="title">タイトル</label></td> 16 <td><input type="text" name="title" th:value="*{title}"/></td></tr> 17 <tr><td><label for="message">メッセージ</label></td> 18 <td><textarea name="message" th:value="*{message}"></textarea></td></tr> 19 <tr><td><label for="mydata">MYDATA_ID</label></td> 20 <td><input type="text" name="mydata" th:value="*{mydata}"/></td></tr> 21 <tr><td></td><td><input type="submit" /></td></tr> 22 </form> 23 </table> 24 <hr/> 25 <table> 26 <tr><th>ID</th><th>名前</th><th>タイトル</th></tr> 27 <tr th:each="obj:${datalist}"> 28 <td th:text="${obj.id}"></td> 29 <td th:text="${obj.mydata.name}"></td> 30 <td th:text="${obj.title}"></td> 31 </table> 32</body> 33</html>
java
1package com.tuyano.springboot; 2 3import java.util.*; 4import javax.annotation.*; 5import javax.persistence.*; 6import javax.servlet.http.HttpServletRequest; 7import javax.validation.*; 8 9import org.springframework.validation.*; 10import org.springframework.beans.factory.annotation.*; 11 12import org.springframework.stereotype.*; 13import org.springframework.web.bind.annotation.*; 14import org.springframework.ui.Model; 15import org.springframework.web.servlet.*; 16 17import com.tuyano.springboot.repositories.*; 18import org.springframework.transaction.annotation.*; 19import org.springframework.validation.annotation.*; 20import com.tuyano.springboot.repositories.*; 21 22@Controller 23public class HeloController { 24 25 @Autowired 26 MsgDataRepository repository; 27 28 @PersistenceContext 29 EntityManager entityManager; 30 31 MsgDataDaoImpl dao; 32 33 @GetMapping("/msg") 34 public ModelAndView msg(ModelAndView mav) { 35 mav.setViewName("showMsgData"); 36 mav.addObject("title","Sample"); 37 mav.addObject("msg","sample"); 38 MsgData msgData = new MsgData(); 39 List<MsgData>list = (List<MsgData>)dao.getAll(); 40 mav.addObject("datalist",list); 41 return mav; 42 } 43 44 @PostMapping("/msg") 45 public ModelAndView msgform( 46 @Valid @ModelAttribute MsgData msgdata, 47 Errors result, 48 ModelAndView mav) { 49 if (result.hasErrors()) { 50 mav.setViewName("showMsgData"); 51 mav.addObject("title","sample [ERROR]"); 52 mav.addObject("msg","値を再チェックしてください"); 53 return mav; 54 } else { 55 repository.saveAndFlush(msgdata); 56 return new ModelAndView("redirect:/msg"); 57 } 58 } 59 60 @PostConstruct 61 public void init() { 62 System.out.println("OK"); 63 dao = new MsgDataDaoImpl(entityManager); 64 } 65}
properties
1spring.datasource.url=jdbc:mysql://localhost:3306/jpa_sample 2spring.datasource.username=root 3spring.datasource.password=root 4spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver 5spring.jpa.database=MYSQL 6spring.jpa.hibernate.ddl-auto=update 7
###発生するエラー
org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'id' cannot be found on null
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。