当我使用@ManyToMany 时,我如何获得另一个 class 的价值

How i get value another class when i using @ManyToMany

我有 2 个 class。请求和服务 class.

请求class。


import com.eziz.clients.Clients;
import com.eziz.services.Services;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

@Entity
@Table(name = "request")
public class Requests {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long requestId;

    @Column(nullable = false, length = 80)
    private String requestComment;

    @Column(nullable = false, length = 15)
    private String requestStatus;

    @DateTimeFormat(pattern = "dd-MM-yyyy")
    @Column(nullable = false)
    private String requestExpiryTime;

    @DateTimeFormat(pattern = "dd-MM-yyyy")
    @Column(nullable = false)
    private String requestDateCreated;

    @ManyToOne
    @JoinColumn(name = "requestClientId")
    private Clients client;

    @ManyToMany
    @JoinColumn(name = "requestServiceId")
    private Set<Services> service = new HashSet<>();

//    @ManyToOne
//    @JoinColumn(name = "requestService")
//    private Services services;
//
    public Long getRequestId() {
        return requestId;
    }

    public void setRequestId(Long requestId) {
        this.requestId = requestId;
    }

    public String getRequestComment() {
        return requestComment;
    }

    public void setRequestComment(String requestComment) {
        this.requestComment = requestComment;
    }

    public String getRequestStatus() {
        return requestStatus;
    }

    public void setRequestStatus(String requestStatus) {
        this.requestStatus = requestStatus;
    }

    public String getRequestExpiryTime() {
        return requestExpiryTime;
    }

    public void setRequestExpiryTime(String requestExpiryTime) {
        this.requestExpiryTime = requestExpiryTime;
    }

    public String getRequestDateCreated() {
        return requestDateCreated;
    }

    public void setRequestDateCreated(String requestDateCreated) {
        this.requestDateCreated = requestDateCreated;
    }

    public Clients getClient() {
        return client;
    }

    public void setClient(Clients client) {
        this.client = client;
    }

    public Set<Services> getService() {
        return service;
    }

    public void setService(Set<Services> service) {
        this.service = service;
    }
}

服务Class.

package com.eziz.services;


import javax.persistence.*;

@Entity
@Table(name = "service")
public class Services {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long serviceId;

    @Column(nullable = false, length = 20)
    private String serviceName;

    @Column(nullable = false, length = 30)
    private String serviceCategory;

    @Column(nullable = false)
    private double servicePrice;

    public Long getServiceId() {
        return serviceId;
    }

    public void setServiceId(Long serviceId) {
        this.serviceId = serviceId;
    }

    public String getServiceName() {
        return serviceName;
    }

    public void setServiceName(String serviceName) {
        this.serviceName = serviceName;
    }

    public String getServiceCategory() {
        return serviceCategory;
    }

    public void setServiceCategory(String serviceCategory) {
        this.serviceCategory = serviceCategory;
    }

    public double getServicePrice() {
        return servicePrice;
    }

    public void setServicePrice(double servicePrice) {
        this.servicePrice = servicePrice;
    }

    @Override
    public String toString() {
        return this.serviceName;
    }
}

现在我在服务 class 中获得了带有 ManyToMany 的 serviceName。 但是我无法得到另一个 value.i 我需要在没有 toString 的情况下这样做。

<div class="modal header">
    <h5 class="modal-title" id="modalTitle">Request Details</h5>
    <button aria-label="Close" class="close" data-dismiss="modal"
            type="button">
        <span aria-hidden="true">&times;</span>
    </button>
</div>

<div class="content content-fixed content-auth">
    <div class="container">
        <h1 class="tx-color-01 mg-b-5" align="center">
            SIFARIŞ MƏLUMATLARI </h1>
        <hr>
        <table class="table">
            <tr th:object="${request}">
            <tr>
                <th scope="col">Sifariş ID</th>
                <td scope="col" data-label="Sifariş ID" th:text="${request.requestId}"></td>
            </tr>
            <tr>
                <th scope="col">Qeydiyyat tarixi</th>
                <td data-label="Qeydiyyat tarixi" th:text="${request.requestDateCreated}"></td>
            </tr>
            <tr>
                <th scope="col">Xidmət</th>
                <td data-label="Xidmət" th:text="${request.service}"></td>
            </tr>
            <tr>
                <th scope="col">Təhvil vermə zamanı</th>
                <td data-label="Təhvil vermə zamanı" th:text="${request.requestExpiryTime}"></td>
            </tr>
            <tr>
                <th scope="col">Rəy</th>
                <td data-label="Rəy" th:text="${request.requestComment}"></td>
            </tr>
            <tr>
                <th scope="col">Status</th>
                <td data-label="Status" th:text="${request.requestStatus}"></td>
            </tr>
            </tr>
        </table>
    </div>
</div>


<div class="modal-footer">
    <button type="button" class="btn btn-danger" data-dismiss="modal">Bağla</button>
</div>

我这样做时无法获得价值。

  <tr>
                <th scope="col">Xidmət</th>
                <td data-label="Xidmət" th:text="${request.service.serviceCategory}"></td>
            </tr>

<tr>
                <th scope="col">Xidmət</th>
                <td data-label="Xidmət" th:text="${request.service.serviceCount}"></td>
            </tr>

我该怎么做?我如何获得 request.service.serviceCount 或 serviceCategory 或 serviceName 当我做 request.service 这只会出现 serviceName,因为我在这里写了 toString 方法 return this.serviceName 。我可以用 toString 方法获得所有这些。但是我需要分开。

这是我的 Thymeleaf 示例

<p>
  <span th:each="task : ${foo.tasks}">
     <span th:text="${taskStat.index} + ': ' + ${task.name}"></span>
  </span>
</p> 



<tr th:each="book : ${listBooksTest}">
        <td th:text="${book.id}">ID Book</td>
        <td th:text="${book.id_author}">ID Author</td>
        <td th:text="${book.title}">Title</td>
        </td>
</tr>
   @GetMapping("/requests/view/{requestId}")
    public String requestDetails(@PathVariable(value = "requestId") long requestId, Model model) {

        Requests request = requestService.getRequestById(requestId);

        List<Clients> listClients = clientService.getAllClients();
        List<Services> listServices = serviceRepository.findAll();

        model.addAttribute("listServices", listServices);
        model.addAttribute("listClients", listClients);

        model.addAttribute("request", request);

        return "request_detail_modal";
    }

上面我是作为列表带过来的,这里应该是带过来td的

            <tr>
                <th scope="col">Xidmət</th>
                <td data-label="Xidmət" th:field="*{service}" th:text="{}"></td>
            </tr>