【问题标题】:How i get value another class when i using @ManyToMany当我使用@ManyToMany 时如何获得另一个类的价值
【发布时间】:2021-08-08 13:10:07
【问题描述】:

我有 2 节课。请求和服务类。

请求类。


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;
    }
}

服务类。

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;
    }
}

现在我在 Service 类中使用 ManyToMany 获得 serviceName。 但我无法获得另一个值。我需要在没有 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 方法返回 this.serviceName 。我可以用 toString 方法得到所有这些。但我需要单独。

【问题讨论】:

  • 你想做什么?我不明白
  • @Zaur mən 服务类-ını 请求类-ında istifadə etmişəm və 请求yaradanda olan servisləri seçirəm。 O servisləri görmək üçün də, request.service yazıb çağırıram。 amma mənə lazımdır ki, request.service.serviceName və ya request.service.serviceCategory hamısın ayrılıqda çağıra bilim。 Çünki request.service yazanda yalnız toString ilə yazdığım qayıdır。 Amma mənə ayrı-ayrılıqda lazımdır.
  • qardas sen reuqest.service.getServiceName yazib cagirandada hamisi gelir ?
  • he basa dusdum eziz
  • getter evezine sen onu bri basa yazirsan

标签: java spring-boot jpa orm spring-data-jpa


【解决方案1】:

这是我的 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>

【讨论】:

    【解决方案2】:
       @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";
        }
    

    Yuxarıda list kimi gətirmişəm, onu burada td-ə gətirmək lazımdı.

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

    【讨论】:

    • yeni service elave edecent tr de each kimi fso
    • mence biraz ozun yoxla alinacaq ozune guven :))))
    • yoxladım elə, alındı 。 Çox sağ ol köməyin üçün。 Bunu yazırdım pozurdum , çünki qırmızılayırdı bunu intellij, deyirdim düz deyiı。他运行 etməmişdim。运行 etdim alındı :D 。 Çox sağ ol təkrar köməyin üçün。
    • 购买 qardas Xosdu
    猜你喜欢
    • 2014-11-26
    • 1970-01-01
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    • 2010-12-13
    • 2019-12-11
    • 1970-01-01
    • 2020-05-16
    相关资源
    最近更新 更多