6

1 対多の関係があります。ProductCategory には多くの製品を含めることができます。これはコードです:

@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;
    }

}

これは、2 つのエンティティを使用するサーブレット コードです。

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

そして、これはエラーです:

java.lang.IllegalStateException: 同期中に、cascade PERSIST とマークされていない関係を介して新しいオブジェクトが見つかりました

なぜこのエラーが発生するのですか? フィールドを「cascade = CascadeType.All」とマークしました!

4

1 に答える 1

10

商品を保存しようとしています。そして、この商品はカテゴリーにリンクされています。したがって、JPA が製品を保存するときは、そのカテゴリがすでに存在している必要があります。または、製品の永続化がそのカテゴリの永続化にカスケードするようにカスケードが構成されている必要があります。

しかし、あなたはそのようなカスケードを持っていません。あなたが持っているのは、カテゴリに対して行われた操作がその製品のリストにカスケードされることを示すカスケードです。

于 2013-03-17T09:56:26.563 に答える