JPA cascading persistent error

I have a one - to - many relationship: product category can contain many products This is the 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<Product> products;

    public String getCategoryName() {
        return categoryName;
    }

    public void setCategoryName(String productName) {
        this.categoryName = productName;
    }

    public List<Product> getProducts() {
        return products;
    }

    public void setProducts(List<Product> products) {
        this.products = products;
    }

}

This is the servlet code using two entities:

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

This is an error:

java. Lang. IllegalStateException: during synchronization, a new object persist is found through a relationship that is not marked as cascading

Why did this error happen? I marked this field as "cascade = cascadetype. All"!

Solution

You are trying to save the product This product is associated with a category Therefore, when JPA saves a product, its category must already exist, or cascade must be configured to persist the product cascade to maintain its category

But you don't have such a cascade What you have is a cascade, which means that any operation on a category will be cascaded to its product list

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>