简体   繁体   English

如何在 Spring Boot 中通过外键列出另一个表中的列

[英]How to list column from another table via foreign key in Spring Boot

I'm struggling with this problem.我正在努力解决这个问题。 I have table "Cities" which has foreign key to table "Countries" with country_id referenced to country from which is city.我有表“城市”,它有表“国家”的外键,其中 country_id 引用城市所在的国家/地区。 In my web application I can list all the data from "Cities" table but I can't find a way to list name of country.在我的 web 应用程序中,我可以列出“城市”表中的所有数据,但我找不到列出国家/地区名称的方法。 This is my service class method.这是我的服务 class 方法。

    public List<City> listAll() {
    List<City> cities = repo.findAll();
    return cities;
}

In "City" entity I have field Country by which I can find in method name of country but I don't know how to return it together with cities.在“城市”实体中,我有字段国家,我可以通过它在国家的方法名称中找到但我不知道如何将它与城市一起返回。

Addition:添加:

    @GetMapping("/cities")
public String getAllCities(Model model) {
    List<City> listCities = service.listAll();

    model.addAttribute("showListCities", listCities);
    return "cities";
}

City.java:城市.java:

package com.bookflight.BookFlight.gradovi;

import com.bookflight.BookFlight.drzave.Drzave;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "cities")
public class City {
    @Id
    @Column(name = "city_id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    @Column(nullable = false, length = 45, name = "city_name")
    private String city_name;
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "cou_id", referencedColumnName = "cou_id")
    private Country countries;

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getcity_name() {
    return city_name;
}

public void setcity_name(String city_name) {
    this.city_name = city_name;
}

public Countries getCountries() {
    return countries;
}

public void setCountries(Country countries) {
    this.countries = countries;
}

} }

NOTE: Every variable name here is in my native language so I literally translated it word by word to better understand your solution afterwards.注意:这里的每个变量名称都是我的母语,所以我逐字逐句翻译它以便之后更好地理解您的解决方案。

You have add FetchType to the @ManyToOne annotation arguments:您已将 FetchType 添加到 @ManyToOne 注释 arguments:

@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "cou_id", referencedColumnName = "cou_id")
private Country countries;

and her a short description for each fetch type:以及她对每种获取类型的简短描述:

FetchType.LAZY will only fire for primary table. FetchType.LAZY只会为主表触发。 If in your code you call any other method that has a parent table dependency then it will fire query to get that table information.如果在您的代码中调用任何其他具有父表依赖项的方法,那么它将触发查询以获取该表信息。

FetchType.EAGER will create join of all table including relevant parent tables directly. FetchType.EAGER将直接创建包括相关父表在内的所有表的连接。

And you can add a method in your city Class to return your country name and this method will be available in your view-layer:你可以在你的城市 Class 添加一个方法来返回你的国家名称,这个方法将在你的视图层中可用:

  public String getCountryName(){
    return countries == null ? null : countries.getName();
    //not sure how the country class is implemented
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Spring Boot中另一张表的外键从一张表中获取值? - How to get the value from one table using the foreign key from another table in Spring boot? 使用 JPA 使用外键连接来自另一个表的列 - Join column from another table with Foreign Key using JPA 有没有办法 map 一个表有一个列,该列在 Spring 引导中具有一个特定实体的多个外键 - Is there a way to map a table that has a column which has more than one foreign key to a specific entity in Spring boot 使用外键从 spring 引导 jpa 中的引用表中查找所有数据 - Find all data using foreign key from referenced table in spring boot jpa 使用 {spring boot & Thymeleaf} 对表中的外键元素进行排序 - Sorting foreign key element in table using {spring boot & Thymeleaf} 如何使用rest api插入hibernate中的表,并且该表2列是另一个表的外键? - How to insert into a table in hibernate using rest api and on that table 2 column are foreign key of another table? 如何在spring boot中将数据从表复制到另一个? - How do I copy data from on table to another in spring boot? 如何阻止 Spring 启动 Jpa 在启动时向数据库视图添加外键约束? - How to stop Spring boot Jpa from adding foreign key constraints to the database views on the startup? 如何将列表从一项服务复制到另一项(Spring Boot) - how to copy List from one Service to another (Spring Boot) 如何在外键 oneToOne Spring 引导中没有 null? - how to not have null in foreign key oneToOne Spring Boot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM