我已經花了幾個小時了,我似乎可以解決這個問題。我有兩個實體產品和客戶,其中一個客戶可以擁有一種產品,而一個產品可能有多個客戶。在我的 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; }
添加回答
舉報
0/150
提交
取消