所有者实体: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 问题,则它工作正常。
有人可以帮我解决这个错误吗?
编辑: Controller :
@RestController
public class PriceListItemControllerTest {
@Autowired
private PriceListItemRepositoryTest priceListItemRepositoryTest;
@GetMapping(value="/getByItemIdTest")
public List<PriceListItemTest> getByItemIdTest(@RequestParam("itemId") String itemId) {
return priceListItemRepositoryTest.findByItemId(itemId);
}
}
最佳答案
像下面这样改变你的模型类:
@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 会自动序列化。
关于java - Spring-Data-Jpa 中一对多映射的 JSON 结果错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52951336/