无法让spring boot hibernate rest api返回一对多关系
Posted
技术标签:
【中文标题】无法让spring boot hibernate rest api返回一对多关系【英文标题】:Cant get spring boot hibernate rest api to return a one to many relationship 【发布时间】:2022-01-20 18:08:48 【问题描述】:我用建筑模型和地板模型为停车场建模。建筑物和楼层之间存在一对多的关系。我已经建立了一个休息控制器来检索数据。我正在尝试通过对api/v1/parkingbuildings/1/
的简单获取请求来检索数据。问题是,在检索建筑物时,根据我的关系映射,我没有看到楼层列表。对我可能犯的任何错误的任何见解将不胜感激。下面是返回的json;
"building_id":1,"building_name":"Labadiestad","postcode":"SA78BQ","max_floors":14,"owner_name":"Schaefer, Gutmann and Braun"
我希望在有效载荷中看到一系列楼层,但我无法理解为什么,我编写了其他类似的更简单的解决方案,它们可以毫无问题地做同样的事情,我比较了我以前的解决方案,发现我的方法没有什么不同。
这是我的建筑模型
package com.admiral.reslink.models;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity(name = "parking_buildings")
@JsonIgnoreProperties("hibernateLazyInitializer", "handler")
public class ParkingBuilding
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long building_id;
private String building_name;
private String postcode;
private int max_floors;
private String owner_name;
// ToDo sort the relationships
@OneToMany(mappedBy = "parkingBuilding")
@JsonIgnore
private List<ParkingFloor> parkingFloors;
public ParkingBuilding()
public long getBuilding_id()
return building_id;
public void setBuilding_id(long building_id)
this.building_id = building_id;
public String getBuilding_name()
return building_name;
public void setBuilding_name(String building_name)
this.building_name = building_name;
public String getPostcode()
return postcode;
public void setPostcode(String postcode)
this.postcode = postcode;
public int getMax_floors()
return max_floors;
public void setMax_floors(int max_floors)
this.max_floors = max_floors;
public String getOwner_name()
return owner_name;
public void setOwner_name(String owner_name)
this.owner_name = owner_name;
public List<ParkingFloor> getParkingFloors()
return parkingFloors;
public void setParkingFloors(List<ParkingFloor> parkingFloors)
this.parkingFloors = parkingFloors;
这是我的地板模型
package com.admiral.reslink.models;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import javax.persistence.*;
import java.util.List;
@Entity
@Table(name = "parking_floors")
@JsonIgnoreProperties("hibernateLazyInitializer", "handler")
public class ParkingFloor
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long floor_id;
private int floor_number;
private int max_height_inches;
private boolean is_covered;
private boolean is_disabled_access;
// ToDo sort the relationships
@ManyToOne
@JoinColumn(name="building_id", nullable=false)
private ParkingBuilding parkingBuilding;
@OneToMany(mappedBy = "parkingFloor")
@JsonIgnore
private List<ParkingSpace> parkingSpace;
public ParkingFloor()
public long getFloor_id()
return floor_id;
public void setFloor_id(long floor_id)
this.floor_id = floor_id;
public int getFloor_number()
return floor_number;
public void setFloor_number(int floor_number)
this.floor_number = floor_number;
public int getMax_height_inches()
return max_height_inches;
public void setMax_height_inches(int max_height_inches)
this.max_height_inches = max_height_inches;
public boolean isIs_covered()
return is_covered;
public void setIs_covered(boolean is_covered)
this.is_covered = is_covered;
public boolean isIs_disabled_access()
return is_disabled_access;
public void setIs_disabled_access(boolean is_disabled_access)
this.is_disabled_access = is_disabled_access;
public ParkingBuilding getParkingBuilding()
return parkingBuilding;
public void setParkingBuilding(ParkingBuilding parkingBuilding)
this.parkingBuilding = parkingBuilding;
public List<ParkingSpace> getParkingSpace()
return parkingSpace;
public void setParkingSpace(List<ParkingSpace> parkingSpace)
this.parkingSpace = parkingSpace;
这是我的建筑控制器
package com.admiral.reslink.controllers;
import com.admiral.reslink.models.ParkingBuilding;
import com.admiral.reslink.models.ParkingFloor;
import com.admiral.reslink.repositories.ParkingBuildingRepository;
import com.admiral.reslink.repositories.ParkingFloorRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/v1/parkingbuildings")
public class ParkingBuildingController
@Autowired
private ParkingBuildingRepository parkingBuildingRepository;
@GetMapping
public List<ParkingBuilding> list() return parkingBuildingRepository.findAll();
@GetMapping
@RequestMapping("id")
public ParkingBuilding get(@PathVariable Long id) return parkingBuildingRepository.getById(id);
@PostMapping
public ParkingBuilding create(@RequestBody final ParkingBuilding parkingBuilding)
return parkingBuildingRepository.saveAndFlush(parkingBuilding);
@RequestMapping(value="id", method = RequestMethod.DELETE)
public void delete(@PathVariable Long id)
parkingBuildingRepository.deleteById(id);
@RequestMapping(value="id", method = RequestMethod.PUT)
public ParkingBuilding update(@PathVariable Long id, @RequestBody ParkingBuilding parkingBuilding)
ParkingBuilding existingParkingBuilding = parkingBuildingRepository.getById(id);
BeanUtils.copyProperties(parkingBuilding, existingParkingBuilding, "building_id");
return parkingBuildingRepository.saveAndFlush(existingParkingBuilding);
【问题讨论】:
可能是一个错误,但您在问题中写的building
和 floor
模型是相同的。
#1 我们还需要看看你是如何查询 ParkingBuilding 的:hibernate、spring 存储库等 #2 你能打印出parkingFloors 的值,然后将其作为响应发送吗?
【参考方案1】:
不确定您是如何检索地板的。 OneToMany 默认为lazy
,除非被询问,否则不会加载。
在您的存储库中,尝试:
@EntityGraph(attributePaths = "parkingFloors")
ParkingBuilding findById(long id);
【讨论】:
以上是关于无法让spring boot hibernate rest api返回一对多关系的主要内容,如果未能解决你的问题,请参考以下文章
无法使用 Hibernate 在 Spring Boot 中将 MongoDB 设置为自动递增
Spring Boot Webapp:让 Hibernate 使用 log4j
无法从 Spring Boot REST 中的 Hibernate POJO 返回 JSON
无法让 liquibase-hibernate 插件与 gradle 一起工作
当加载 spring-boot 和 spring-data-jpa 时,Hibernate 无法加载 JPA 2.1 Converter