JPA级联持续错误

前端之家收集整理的这篇文章主要介绍了JPA级联持续错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一对多关系:ProductCategory可以包含许多产品.这是代码
  1. @Entity
  2. public class Product implements Serializable {
  3.  
  4. @Id
  5. @GeneratedValue(strategy=GenerationType.IDENTITY)
  6. private String id;
  7. @Column(name="ProductName")
  8. private String name;
  9. private BigDecimal price;
  10. private String description;
  11. @ManyToOne
  12. @JoinColumn(name="UserId")
  13. private User user;
  14. @ManyToOne
  15. @JoinColumn(name="Category")
  16. private ProductCategory category;
  17. private static final long serialVersionUID = 1L;
  18.  
  19. public Product() {
  20. super();
  21. }
  22. public String getId() {
  23. return this.id;
  24. }
  25.  
  26. public void setId(String id) {
  27. this.id = id;
  28. }
  29. public String getName() {
  30. return this.name;
  31. }
  32.  
  33. public void setName(String name) {
  34. this.name = name;
  35. }
  36. public BigDecimal getPrice() {
  37. return this.price;
  38. }
  39.  
  40. public void setPrice(BigDecimal price) {
  41. this.price = price;
  42. }
  43. public String getDescription() {
  44. return this.description;
  45. }
  46.  
  47. public void setDescription(String description) {
  48. this.description = description;
  49. }
  50. public User getUser() {
  51. return this.user;
  52. }
  53.  
  54. public void setUser(User user) {
  55. this.user = user;
  56. }
  57. public ProductCategory getCategory() {
  58. return this.category;
  59. }
  60.  
  61. public void setCategory(ProductCategory category) {
  62. this.category = category;
  63. }
  64. }
  65.  
  66.  
  67. @Entity
  68. public class ProductCategory {
  69. @Id
  70. private String categoryName;
  71. @OneToMany(cascade= CascadeType.ALL,mappedBy="category")
  72. private List<Product> products;
  73.  
  74. public String getCategoryName() {
  75. return categoryName;
  76. }
  77.  
  78. public void setCategoryName(String productName) {
  79. this.categoryName = productName;
  80. }
  81.  
  82. public List<Product> getProducts() {
  83. return products;
  84. }
  85.  
  86. public void setProducts(List<Product> products) {
  87. this.products = products;
  88. }
  89.  
  90. }

这是使用2个实体的Servlet代码

  1. String name = request.getParameter("name");
  2. BigDecimal price = new BigDecimal(request.getParameter("price"));
  3. String description = request.getParameter("description");
  4. ProductCategory category = new ProductCategory();
  5. category.setCategoryName(request.getParameter("category"));
  6. Product product = new Product();
  7. product.setName(name);
  8. product.setPrice(price);
  9. product.setDescription(description);
  10. product.setCategory(category);
  11. User user = userManager.findUser("Meow");
  12. product.setUser(user);
  13. productManager.createProduct(product); // productManager is an EJB injected by container

这是错误

java.lang.IllegalStateException:在同步期间,通过未标记为级联的关系找到新对象PERSIST

为什么会发生这种错误?我将该字段标记为“cascade = CascadeType.All”!

解决方法

您正在尝试保存产品.此产品与一个类别相关联.因此,当JPA保存产品时,其类别必须已经存在,或者必须配置级联,以便持久保持产品级联以保持其类别.

但是你没有这样的级联.你所拥有的是一个级联,表示对某个类别进行的任何操作都会级联到其产品列表中.

猜你在找的Java相关文章