JPA如何从填充了ManyToMany关系中的对象的Arraylist中获取单个属性

时间:2019-05-30 21:29:45

标签: java spring hibernate spring-mvc jpa

在我的应用程序中,我有一个双向ManyToMany关系中的Meal和Dish模型。像下面这样:

Meal.Java

public class Meal {

    @Id
    @GeneratedValue
    private Long id;

    @ManyToMany
    private Set<Dish> mealDishes = new HashSet<>();

Dish.Java

public class Dish {

    @Id
    @GeneratedValue
    private Long id;

    private String dishName;

    @ManyToMany(mappedBy = "mealDishes")
    private Set<Meal> dishMeals = new HashSet<>();

可将一道菜添加到餐中。现在,在我的视图中,我想显示已添加到餐中的dishName属性。我不知道如何访问该属性,现在我只能得到一个内存地址,就像在这里看到的那样:https://ibb.co/K7Kn5Vx

我的列表视图如下:

    <tr th:each="meal : ${mealPage}">
        <td th:text="${meal.id}">1</td>
        <td th:text="${#calendars.format(meal.created)}">July 11,
            2012 2:17:16 PM CDT</td>
        <td th:text="${meal.mealName}">Friet</td>
        <td th:text="${meal.getMealDishes()}">Friet</td>
        <!-- <td th:text="${meal.mealPrice}">12</td> -->
        <td th:text="${meal.mealType}">Avondmaal</td>
        <td th:text="${meal.mealPrice}">3</td>
        <td th:text="${meal.mealCook.getStudentName()}">Henk</td>
        <td><a href="view.html" th:href="@{'/' + ${meal.id}}"
               th:text="${meal.mealSummary}"> The summary </a></td>
    </tr>

我正在尝试通过$ {meal.getMealDishes()标记获取菜,但这显然是错误的。任何帮助将不胜感激!

0 个答案:

没有答案