[英]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.