複数のテーブルが関連して動かしたい場合は、「アソシエーション」という機能によりエンティティ同士を連携して処理する
@OneToOne,@OneToMany, @ManyToOne, @ManyToMany
package com.example.demo; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.ManyToOne; import javax.persistence.Table; import javax.validation.constraints.NotNull; import org.hibernate.validator.constraints.NotEmpty; @Entity @Table(name = "msgdata") public class MsgData { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column @NotNull private long id; @Column private String title; @Column(nullable = false) @NotEmpty private String message; @ManyToOne private MyData mydata; public MsgData() { super(); mydata = new MyData(); } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public MyData getMyData() { return mydata; } public void setMydata(MyData mydata) { this.mydata = mydata; } }
MsgDataRepository.java
package com.example.demo.repositories; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import com.example.demo.MsgData; @Repository public interface MsgDataRepository extends JpaRepository<MsgData, Long>{ }
MsgDataDao.java
package com.example.demo; import java.io.Serializable; import java.util.List; public interface MsgDataDao<T> { public List<MsgData> getAll(); public MsgData findById(long id); }
MsgDataDaoImpl.java
package com.example.demo; import java.util.List; import javax.persistence.EntityManager; import org.springframework.stereotype.Repository; @SuppressWarnings("rawtypes") @Repository public class MsgDataDaoImpl implements MsgDataDao<MsgDataDao>{ private EntityManager entityManager; public MsgDataDaoImpl() { super(); } public MsgDataDaoImpl(EntityManager manager) { entityManager = manager; } @SuppressWarnings("unchecked") @Override public List<MsgData> getAll(){ return entityManager .createQuery("from MsgData") .getResultList(); } @Override public MsgData findById(long id) { return (MsgData)entityManager .createQuery("from MsgData where id = " + id).getSingleResult(); } }
showMsgData.html
<h1 th:text="${title}">MyMsg page</h1> <p th:text="${msg}"></p> <table> <form method="post" action="/msg" th:object="${formModel}"> <input type="hidden" name="id" th:value="*{id}"> <tr> <td><label for="title">タイトル</label></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><input type="text" name="mydata"></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>
MsgDataController.java
package com.example.demo; 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.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.example.demo.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のsampleです"); 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) 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.saveAndFlush(msgdata); return new ModelAndView("redirect:/msg"); } } @PostConstruct public void init() { System.out.println("ok"); dao = new MsgDataDaoImpl(entityManager); } }
んん?あれ?