質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Thymeleaf

Thymeleaf(タイムリーフ)とは、Java用のテンプレートエンジンで、特定のフレームワークに依存せず使用することが可能です。

Q&A

解決済

1回答

9336閲覧

Thymeleaf エラー 対策

yuu_info

総合スコア14

Thymeleaf

Thymeleaf(タイムリーフ)とは、Java用のテンプレートエンジンで、特定のフレームワークに依存せず使用することが可能です。

0グッド

0クリップ

投稿2020/09/06 05:37

前提・実現したいこと

INNER JOIN句で結合したテーブルのデータをThymeleaf上で表示させたいのですが、下記のエラーに悩まされています。

userのuserPListにThymeleaf上でアクセスする方法

発生している問題・エラーメッセージ

org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'plan' cannot be found on object of type 'org.hibernate.collection.internal.PersistentBag' - maybe not public or not valid?

Thymeleaf

<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <title>テス</title> <link href="/css/tes.css" rel="stylesheet"></link> <meta charset="utf-8" /> </head> <body> <h1>ユーザー</h1> <table> <thead> <tr> <th>ID</th> <th>名前</th> <th>予定</th> <th>日付</th> </tr> </thead> <tbody> <tr th:each = "user : ${userdata}" th:object="${user}"> <td class="center" th:text="${user.id}"></td> <td th:text="${user.name}"></td> <td th:text="${user.userPList.plan}"></td> <td th:text="${user.date}"></td> </tbody> </table> </body> </html>

Entity

package com.example.springsample.entity; import java.io.Serializable; import java.util.Date; import java.util.List; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Table; import lombok.Data; @Entity @Data @Table(name="user") public class User implements Serializable { //ユーザー情報のエンティティ @Id @Column(name="id") @GeneratedValue(strategy=GenerationType.IDENTITY) private Long id; @Column(name="name") private String name; @Column(name="address") private String address; @Column(name="phone") private String phone; @Column(name="update_date") private Date updateDate; @Column(name="create_date") private Date createDate; @Column(name="delete_date") private Date deleteDate; @OneToMany(mappedBy = "user", cascade = CascadeType.ALL) private List<UserPlan> userPList; }

Entity

package com.example.springsample.entity; import java.io.Serializable; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; import lombok.Data; @Entity @Data @Table(name="user_plan") public class UserPlan implements Serializable { //ユーザー予定情報のエンティティ @Id @Column(name="plan_id") private Long plan_id; @Column(name="plan") private String plan; @Column(name="date") private Date date; @ManyToOne @JoinColumn(name="id") private User user; }

Controller

package com.example.springsample.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; 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.bind.annotation.RequestParam; import com.example.springsample.dto.UserRequest; import com.example.springsample.entity.User; import com.example.springsample.service.UserService; @Controller public class UserController { @Autowired UserService userService; @RequestMapping(value = "/user/list", method = RequestMethod.GET)//user/listにアクセスがあったときユーザー情報を抜き出す public String displayList(Model model) { List<User> userlist = userService.searchAll(); model.addAttribute("userlist", userlist);//modelにSELECTしたデータを追加 return "user/list"; } @RequestMapping(value = "/user/add", method = RequestMethod.GET)//user/addにアクセスがあった時ユーザーリクエストのインスタンスを返す public String displayAdd(Model model) { model.addAttribute("userRequest", new UserRequest()); return "user/add"; } @RequestMapping(value="/user/create", method = RequestMethod.POST) public String create(@ModelAttribute UserRequest userRequest, Model model) { // ユーザー情報の登録 userService.create(userRequest); return "redirect:/user/list"; } @RequestMapping(value = "/user/tes", method = RequestMethod.POST) public String detailModel(Model model, @RequestParam((String) "id") Long id) { //選択ユーザーの抽出 List<User> user = userService.findUser(id); model.addAttribute("userdata", user); return "user/tes"; } }

Repository

package com.example.springsample.repository; import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; import com.example.springsample.entity.User; @Repository public interface UserRepository extends JpaRepository<User, Long> { @Query("SELECT u FROM User u INNER JOIN u.userPList up WHERE u.id = :id") List<User> find(@Param("id") Long id); }

###画像

イメージ説明

補足情報

デバッグでSQL文自体は正しく動いており、データを持ってこれている事は確認できました。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

html

1<tr th:each = "user : ${userdata}" th:object="${user}">

userの子要素:userPListは繰り返し要素(おそらくList)なので、これをそのまま出力はできません。

<td th:text="${user.userPList.plan}"></td> <tr th:each>で行ったのと同様に、さらにeachを使って繰り返し出力が必要でしょうか。 ``` <td th:each="plan : ${user.userPList}" th:text="${plan}"></td> ``` ただしこのままですと、planの数だけtd要素が出力されてしまいますので、<td>の数が合わないと思われます。 出力したいタグに合わせて、eachタグをさらに子要素にするかは検討が必要です。

投稿2020/09/06 05:52

編集2020/09/06 07:41
A-pZ

総合スコア12011

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yuu_info

2020/09/06 06:08

ご回答ありがとうございます!ご指摘いただいた点を修正しましたが、相変わらず同じ内容のエラーが出ています・・・
A-pZ

2020/09/06 07:41

失礼しました。さらに繰り返し要素があったのですね、回答を編集しましたのでご確認ください。
yuu_info

2020/09/06 10:45

仰る通り、userPListはList型です。User一人のデータに対してUserPList(予定)が複数存在する予定管理表のようなものにしたいと考えておりました。 List型のUserの子要素に更にListがあった場合、th:eachを二重にしなければアクセス出来ないんですね・・・
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問