5
일대 다 관계 : ProductCategory에 많은 제품이 포함될 수 있습니다.JPA 캐스케이드 지속 오류
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 : 동기화 새로운 중에이 2 엔티티를 사용하는 서블릿 코드가
@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;
}
}
입니다 :이 코드입니다 개체가 계단식으로 표시되지 않은 관계를 통해 발견되었습니다. PERSIST
왜이 오류가 발생합니까? 필드를 "cascade = CascadeType.All"으로 표시했습니다!
그래서 어떻게해야합니까? @OneToMany (cascade = CascadeType.PERSIST, mappedBy = "category") 코드를 변경하면 개인 목록 제품; 도 작동하지 않습니다. 동일한 오류가 발생합니다. –
캐스케이드가 연결의 잘못된쪽에 있습니다. 당신은 JPA에 다음과 같이 말하고 있습니다. * 범주를 지속 할 것을 요구할 때 * 또한 해당 제품을 지속시킵니다 *. 그러나 당신은 JPA에 범주를 유지하도록 요청하지 않습니다. 당신은 제품을 지속하도록 요구합니다. 그리고 제품이 지속될 때 카테고리가 지속되어야한다는 말은 어디에도 없습니다. –
캐스케이드 표기법이 어느쪽에 있다고 생각합니까? 내가 아는 한 다 대일 관계에서 항상 반대면에 있습니다 (한면 또는 다른 말로, @OneToMany 주석이 포함 된면을 의미 함). –