我已经花了几个小时了,我似乎可以解决这个问题。我有两个实体产品和客户,其中一个客户可以拥有一种产品,而一个产品可能有多个客户。在我的 SQL SERVER Management studio 中,Product 表的主键作为 Customer 表中的外键。
我在下面的代码中展示了这两个实体。问题在于,客户“c”被递归地附加到“myproducts”,这是当我在浏览器窗口上检查控制台时显示的 JSON 中的mappedBy 属性。(请参阅下面错误中的嵌套对象“myproducts”和“c”)
我正在使用 GET 方法 API 在屏幕上显示客户。
Products.java
@Entity
@Table(name="NewProductDetails")
public class Products{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "p_id")
private int productId;
@Size(max=65)
@Column(name = "p_name")
private String name;
@Column(name = "p_price")
private int price;
@OneToMany(fetch = FetchType.LAZY,
cascade = CascadeType.ALL,
mappedBy = "myproduct")
public Set<Customer> c;
public Products() {
}
public Products(String p_name, int p_price) {
this.name = p_name;
this.price = p_price;
}
public long getproductId() {
return productId;
}
public void setproductId(int id) {
this.productId = id;
}
public void setPName(String p_name) {
this.name = p_name;
}
public String getPName() {
return this.name;
}
public void setPrice(int p_price ) {
this.price = p_price ;
}
public int getPrice() {
return this.price;
}
}
ProductController.java
@CrossOrigin(origins = "http://localhost:4200")
@RestController
@RequestMapping("/api")
public class ProductController {
@Autowired
ProductRepository productRepository;
@GetMapping("/product")
public List<Products> getAllProducts(){
System.out.println("Get All the product .... ");
List<Products> products = new ArrayList<>();
productRepository.findAll().forEach(products :: add);
return products;
}
慕妹3242003
相关分类