JPA关联表不可删除
我对JPA(日食)有问题。 我无法删除关联表。情况就是这样:JPA关联表不可删除,jpa,eclipselink,Jpa,Eclipselink,我对JPA(日食)有问题。 我无法删除关联表。情况就是这样: 产品1:n到产品资源 资源1:n到ProductResource 我首先设置ProductResource的product和resource属性。如果我随后尝试删除ProductResource对象,则不会发生任何事情(不会生成sql-没有异常)。如果我注释掉ProductResource中的两个OneToMany注释,我可以删除该对象。我还可以在未设置产品和资源属性时删除该对象。如果我仅注释掉ressource属性上方的注释,则
- 产品1:n到产品资源
- 资源1:n到ProductResource
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>();
公共类产品实现可序列化{
@OneToMany(mappedBy=“product”,fetch=FetchType.EAGER,cascade=CascadeType.ALL)
private List productResources=new ArrayList();
资源:
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 Resource implements Serializable {
@OneToMany(mappedBy="resource", fetch=FetchType.EAGER, cascade=CascadeType.ALL)
private List<ProductResource> productResources = new ArrayList<ProductResource>();
公共类资源实现可序列化{
@OneToMany(mappedBy=“resource”,fetch=FetchType.EAGER,cascade=CascadeType.ALL)
private List productResources=new ArrayList();
问候Marcel解决方案如下: 产品类别
@PrivateOwned
@OneToMany(mappedBy="product", fetch=FetchType.EAGER, cascade=CascadeType.ALL)
private List<ProductResource> productResources = new ArrayList<ProductResource>();
@PrivateOwned
@OneToMany(mappedBy=“product”,fetch=FetchType.EAGER,cascade=CascadeType.ALL)
private List productResources=new ArrayList();
资源类
@PrivateOwned
@OneToMany(mappedBy="resource", 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 productResources=new ArrayList();
@私有的是新的
Marcel实际上有3种解决方案: 1) 在删除ProductResource对象之前删除孤立对象。ProductResource未被删除的原因是系统中仍有引用它们的对象 2) 删除孤立对象中对ProductResource对象的引用。原因与上述相同
3) 将产品和资源对象设置为使用JPA批注。这将导致孤立对象(如果存在)被自动删除。这是您可能希望或不希望为您自动执行的行为。原因可能是产品或资源对象不需要存在对ProductResource的引用。这取决于nds介绍您的设计。非常感谢您的详细解释。起初,JPA看起来很容易使用。细节是关键。很好,像您这样的人花时间帮助像我这样的新手。问候Marcel