title: JPA Cascade Persist Error
body: <p>I have a One-to-Many relationship: A ProductCategory can contains many Product. This is the code:</p>
  
  <pre><code>@Entity
  public class Product implements Serializable {
  
      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      private String id;
      @Column(name="ProductName")
      private String name;
      private BigDecimal price;
      private String description;
      @ManyToOne 
      @JoinColumn(name="UserId")
      private User user;
      @ManyToOne
      @JoinColumn(name="Category")
      private ProductCategory category;
      private static final long serialVersionUID = 1L;
  
      public Product() {
          super();
      }   
      public String getId() {
          return this.id;
      }
  
      public void setId(String id) {
          this.id = id;
      }   
      public String getName() {
          return this.name;
      }
  
      public void setName(String name) {
          this.name = name;
      }   
      public BigDecimal getPrice() {
          return this.price;
      }
  
      public void setPrice(BigDecimal price) {
          this.price = price;
      }   
      public String getDescription() {
          return this.description;
      }
  
      public void setDescription(String description) {
          this.description = description;
      }   
      public User getUser() {
          return this.user;
      }
  
      public void setUser(User user) {
          this.user = user;
      }   
      public ProductCategory getCategory() {
          return this.category;
      }
  
      public void setCategory(ProductCategory category) {
          this.category = category;
      }
  }
  
  
  @Entity
  public class ProductCategory {
      @Id
      private String categoryName;
      @OneToMany(cascade= CascadeType.ALL,mappedBy="category")
      private List&lt;Product&gt; products;
  
      public String getCategoryName() {
          return categoryName;
      }
  
      public void setCategoryName(String productName) {
          this.categoryName = productName;
      }
  
      public List&lt;Product&gt; getProducts() {
          return products;
      }
  
      public void setProducts(List&lt;Product&gt; products) {
          this.products = products;
      }
  
  }
  </code></pre>
  
  <p>This is Servlet code which use the 2 entities:</p>
  
  <pre><code>String name = request.getParameter("name");
  BigDecimal price = new BigDecimal(request.getParameter("price"));
  String description = request.getParameter("description");
  ProductCategory category = new ProductCategory();
  category.setCategoryName(request.getParameter("category"));
  Product product = new Product();
  product.setName(name);
  product.setPrice(price);
  product.setDescription(description);
  product.setCategory(category);
  User user = userManager.findUser("Meow");
  product.setUser(user);
  productManager.createProduct(product);  // productManager is an EJB injected by container
  </code></pre>
  
  <p>And this is the error:</p>
  
  <p><strong>java.lang.IllegalStateException: During synchronization a new object was found through a relationship that was not marked cascade PERSIST</strong></p>
  
  <p>Why does this error happen? I marked the field as "cascade = CascadeType.All"!</p>