Spring-Data-Jpa 中一对多映射的 JSON 结果错误
Posted
技术标签:
【中文标题】Spring-Data-Jpa 中一对多映射的 JSON 结果错误【英文标题】:Error in JSON result for one to many mapping in Spring-Data-Jpa 【发布时间】:2019-03-27 19:50:21 【问题描述】:所有者实体:PriceListItemTest.java
@JsonPropertyOrder("pricingUOMCode","lineTypeCode","primaryPricingUOMFlag","priceListId","itemId","priceListItemId")
@Entity
@Table(name = "QP_PRICE_LIST_ITEMS")
public class PriceListItemTest implements Serializable
@Column(name = "price_list_item_id")
private String priceListItemId;
@Column(name = "pricing_uom_code")
private String pricingUOMCode;
@Column(name = "line_type_code")
private String lineTypeCode;
@Column(name = "primary_pricing_uom_flag")
private String primaryPricingUOMFlag;
@Id
@Column(name = "item_id")
private String itemId;
@OneToMany(mappedBy = "priceListItemsTest", cascade = CascadeType.ALL)
private List<ItemDetailTest> itemDetailsTest;
// getters and setters
和Entity2:ItemDetailTest.java
@JsonPropertyOrder("itemNumber","inventoryItemId","organizationId")
@Entity
@Table(name = "egp_system_items_b")
public class ItemDetailTest implements Serializable
@Id
@Column(name = "inventory_item_id")
private String inventoryItemId;
@Column(name = "item_number")
private String itemNumber;
@Column(name = "organization_id")
private String organizationId;
// To be used for bidirectional
@ManyToOne//(fetch = FetchType.LAZY)
private PriceListItemTest priceListItemsTest;
// getters and setters
RestController 在检索 JSON 结果时返回错误。
"timestamp": "2018-10-23T14:05:26.248+0000",
"status": 500,
"error": "Internal Server Error",
"message": "Could not write JSON: could not extract ResultSet; nested exception is com.fasterxml.jackson.databind.JsonMappingException: could not extract ResultSet (through reference chain: java.util.ArrayList[0]->com.Pricing.Pricing_App.model.PriceListItemTest[\"itemDetailsTest\"])",
"path": "/getByItemIdTest"
可能是映射 oneTomany 是错误的。 如果它具有 oneToone 映射但面临 OneToMany 问题,则它工作正常。 有人可以帮我解决错误吗?
编辑: 控制器:
@RestController
public class PriceListItemControllerTest
@Autowired
private PriceListItemRepositoryTest priceListItemRepositoryTest;
@GetMapping(value="/getByItemIdTest")
public List<PriceListItemTest> getByItemIdTest(@RequestParam("itemId") String itemId)
return priceListItemRepositoryTest.findByItemId(itemId);
【问题讨论】:
如果您可以发布您的 REST 控制器和 API 请求以及日志,这将对我们更有帮助。 我已经添加了 Rest Controller 【参考方案1】:使用@JsonIgnore,如下所示。问题是无限的 json 调用正在通过你的 json 调用。忽略在您的 ManytoOne 关系上使用此注释。对我来说很好。
@ManyToOne//(fetch = FetchType.LAZY)
@JsonIgnore
private PriceListItemTest priceListItemsTest;
【讨论】:
【参考方案2】:如下更改您的模型类:
@Entity
@Table(name = "QP_PRICE_LIST_ITEMS")
public class PriceListItemTest implements Serializable
private static final long serialVersionUID = -748956247024967638L;
@Basic
@Column(name = "price_list_item_id")
private String priceListItemId;
@Basic
@Column(name = "pricing_uom_code")
private String pricingUOMCode;
@Basic
@Column(name = "line_type_code")
private String lineTypeCode;
@Basic
@Column(name = "primary_pricing_uom_flag")
private String primaryPricingUOMFlag;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "item_id")
private String itemId;
@JsonManagedReference
@OneToMany(mappedBy = "priceListItemsTest", cascade = CascadeType.ALL)
private List<ItemDetailTest> itemDetailsTest;
// getters and setters
和
@Entity
@Table(name = "egp_system_items_b")
public class ItemDetailTest implements Serializable
private static final long serialVersionUID = 8495817802073010928L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "inventory_item_id")
private String inventoryItemId;
@Basic
@Column(name = "item_number")
private String itemNumber;
@Basic
@Column(name = "organization_id")
private String organizationId;
@JsonBackReference
@ManyToOne
@JoinColumn(name = "fk_item_id", nullable = false) // name of the foreign key column
private PriceListItemTest priceListItemsTest;
// getters and setters
在您的 JoinColumn 中提供外键字段的实际名称。它应该可以工作,因为 Jackson 会自动序列化。
对于一对多的引用,您可以点击以下链接:link1 和 link2
【讨论】:
【参考方案3】:两个实体之间存在双向映射,这会在 JSON 序列化期间导致无限循环。
例如,您必须使用 @JsonIgnore 忽略其中一侧。
在此处阅读有关该主题的更多信息:
https://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion
【讨论】:
以上是关于Spring-Data-Jpa 中一对多映射的 JSON 结果错误的主要内容,如果未能解决你的问题,请参考以下文章