前提・実現したいこと
Specificationでjoinした先のテーブルからSELECTしたデータをthymeleafで出力したいです。
一応値は取ってこれてはいるのですが、この場合どういう記述をすればthymeleafで出力されるでしょうか?
KoinMaster.java
package com.example.demo.koinMaster; import java.util.List; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Table; import com.example.demo.shitenMaster.ShitenMaster; @Entity @Table(name = "koinmaster") public class KoinMaster { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column private long id; @Column private int koinid; @Column private String koinname; @Column private int age; @Column private int shitenid; @Column private int busho; @OneToMany(fetch = FetchType.EAGER, mappedBy="shitenid") private List<ShitenMaster> koinlist; // @ManyToOne(fetch = FetchType.LAZY) // @JoinColumn(name = "shitenid", referencedColumnName = "id", insertable = false, updatable = false) // private KoinMaster koinmaster; public long getId() { return id; } public void setId(long id) { this.id = id; } public int getKoinid() { return koinid; } public void setKoinid(int koinid) { this.koinid = koinid; } public String getKoinname() { return koinname; } public void setKoinname(String koinname) { this.koinname = koinname; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getShitenid() { return shitenid; } public void setShitenid(int shitenid) { this.shitenid = shitenid; } public int getBusho() { return busho; } public void setBusho(int busho) { this.busho = busho; } public List<ShitenMaster> getKoinlist() { return koinlist; } public void setKoinlist(List<ShitenMaster> koinlist) { this.koinlist = koinlist; } }
該当のソースコード
KoinMasterSpecification.java
1package com.example.demo.koinMaster; 2 3import javax.persistence.criteria.CriteriaBuilder; 4import javax.persistence.criteria.CriteriaQuery; 5import javax.persistence.criteria.JoinType; 6import javax.persistence.criteria.Predicate; 7import javax.persistence.criteria.Root; 8 9import org.springframework.data.jpa.domain.Specification; 10import org.springframework.stereotype.Component; 11 12@Component 13public class KoinMasterSpecifications { 14 15 public Specification<KoinMaster> nameContains(int shitenid) { 16 17 return shitenid == 0 ? null : new Specification<KoinMaster>() { 18 19 @Override 20 public Predicate toPredicate(Root<KoinMaster> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) { 21 22 return criteriaBuilder.equal(root.join("koinlist", JoinType.LEFT).get("shitenid"), shitenid); 23 } 24 }; 25 } 26 27} 28
list.html
<!doctype html> <html xmlns:th="http://www.thymeleaf.org"> <head th:replace="~{layout/component :: head('配属情報一覧')}"> <body> <th:block th:replace="~{layout/component :: header}"></th:block> <div class="container"> <th:block th:replace="~{layout/component :: container_top('配属情報一覧')}"></th:block> <div class="row justify-content-start"> <div style="height: 200px; width: 100%; border: solid 1px; margin: 20px;"> <form method="post" th:action="@{/haizokuMaster/list}" th:object="${haizokuMasterListForm}"> <table> <tr> <td> <label>支店名</label> </td> <td> <input type="text" name="shitenid"> </td> </tr> </table> <input type="submit" value="検索" class="btn btn-outline-secondary"> </form> </div> <table class="table table-striped table-sm"> <tr> <th>支店名</th> <th>年齢</th> <th>名前</th> </tr> <tr th:each="entity:${list}"> <td th:text="${entity.shitenname}">name</td> <td th:text="${entity.age}">name</td> <td th:text="${entity.koinname}">name</td> </tr> </table> <a href="/" class="btn btn-outline-secondary">戻る</a> </div> </div> <footer th:replace="~{layout/component :: footer}"></footer> </body> </html>
KoinMaster.java
package com.example.demo.koinMaster; import java.util.ArrayList; import java.util.List; import java.util.Optional; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Sort; import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.example.demo.haizokuMaster.HaizokuMasterListForm; @Service @Transactional public class KoinMasterService { @Autowired KoinMasterRepository koinMasterRepositry; @Autowired KoinMasterSpecifications koinMasterSpecifications; @PersistenceContext EntityManager entityManager; public KoinMaster save(KoinMaster entity) { return this.koinMasterRepositry.save(entity); } public List<KoinMaster> findAll() { return koinMasterRepositry.findAll(Sort.by("id")); } public Optional<KoinMaster> findById(Long id) { return this.koinMasterRepositry.findById(id); } public int findByMaxKoinId() { return (Integer)entityManager .createQuery("select COALESCE(MAX(koinid), 0) from KoinMaster") .getSingleResult(); } public List<KoinMaster> findUsers(HaizokuMasterListForm haizokuMasterListForm) { List<KoinMaster> list = new ArrayList<KoinMaster>(); list = koinMasterRepositry.findAll(Specification.where(koinMasterSpecifications.nameContains(haizokuMasterListForm.getShitenid()))); return list; } }
試したこと
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。