JPA关联表不可删除

时间:2010-03-19 21:03:42

标签: jpa eclipselink

我遇到了JPA(EclipseLink)的问题。 我无法删除关联表。情况就是这样:

  • 产品1:n到ProductResource
  • 资源1:n到ProductResource

我首先设置ProductResource的产品和资源属性。如果我然后尝试删除ProductResource对象没有任何反应(没有生成SQL - 没有例外)。如果我在ProductResource中注释掉OneToMany注释,我可以删除该对象。我还可以在未设置product和resource属性时删除该对象。如果我只注释掉ressource attribut上面的注释,则在删除产品对象时会删除ProductResource对象(cascade = CascadeType.ALL)。我希望有人能给我一个提示。谢谢。

产品资源:

public class ProductResource implements Serializable {
 @ManyToOne(fetch=FetchType.EAGER, cascade=CascadeType.MERGE)
 private Product product;

 @ManyToOne(fetch=FetchType.EAGER, cascade=CascadeType.MERGE)
 private Resource resource;

产品:

public class Product implements Serializable {

 @OneToMany(mappedBy="product", fetch=FetchType.EAGER, cascade=CascadeType.ALL)
 private List<ProductResource> productResources = new ArrayList<ProductResource>();

资源:

public class Resource implements Serializable {

 @OneToMany(mappedBy="resource", fetch=FetchType.EAGER, cascade=CascadeType.ALL)
 private List<ProductResource> productResources = new ArrayList<ProductResource>();

问候马塞尔

2 个答案:

答案 0 :(得分:0)

以下是解决方案:

产品类

@PrivateOwned
@OneToMany(mappedBy="product", fetch=FetchType.EAGER, cascade=CascadeType.ALL)
private List<ProductResource> productResources = new ArrayList<ProductResource>();

资源类

@PrivateOwned
@OneToMany(mappedBy="resource", fetch=FetchType.EAGER, cascade=CascadeType.ALL)
private List<ProductResource> productResources = new ArrayList<ProductResource>();

@PrivateOwned是新的...

马塞尔

答案 1 :(得分:0)

实际上有3个解决方案:

1)在删除ProductResource对象之前删除孤立。 ProductResource未被删除的原因是因为系统中仍有对象引用它们。

2)删除孤儿中对ProductResource对象的引用。这与上述原因相同。

3)使用JPA Annotations将Product和Resource对象设置为@PrivateOwned。这将导致孤儿自动被删除(如果存在)。这是您可能希望或可能不希望自动完成的行为。原因可能是因为Product或Resource对象不需要存在对ProductResource的引用。这取决于你的设计。

相关问题