JPA CascadeType.All不会删除父行和子行 [英] JPA CascadeType.All doesn't delete parent and child rows
问题描述
在JPA或Hibernate中处理级联一直很麻烦,但我现在真的没有得到它。
我想删除父行品牌和子行为引用它。我使用CascadeType.ALL但没有运气。
以下是我的模型(Transact< - TransactProduct - > Product - > Brand - > Customer)关系清楚:
@Entity
@Table(name =customer)
public class Customer {
@OneToMany(fetch = FetchType.EAGER,mappedBy =customer,cascade = CascadeType.ALL)
private Collection< Brand>品牌;
}
@Entity
@Table(name =brand)
public class Brand实现Serializable {
@ManyToOne
@JoinColumn (name =customer_id)
私人客户客户;
@OneToMany(mappedBy =brand,cascade = CascadeType.ALL)
private Collection< Product>产品;
@Entity
@Table(name =product)
public class Product {
@ManyToOne
@JoinColumn(name =brand_id)
私人品牌品牌;
@OneToMany(mappedBy =product,cascade = CascadeType.ALL)
private Collection< TransactProduct> transactProducts;
$ b $ @实体
@Table(name =transact)
public class Transact {
@OneToMany(mappedBy = transact,fetch = FetchType.EAGER,cascade = CascadeType.ALL)
private Collection< TransactProduct> transactProducts;
@ManyToOne
@JoinColumn(name =user_id)
私人用户用户;
@Entity
@Table(name =transact_product)
public class TransactProduct {
@ManyToOne
@JoinColumn(name =product_id)
私人产品产品;
@ManyToOne
@JoinColumn(name =transact_id);
私人交易交易;
}
品牌库:
@Repository
public interface BrandRepository扩展JpaRepository< Brand,Long> {}
在我的控制器中,我想删除这样的品牌:
品牌品牌= brandRepository.findOne(id);
brandRepository.delete(品牌);
在findOne将它写入控制台之后:
select * from brand brand0_ left outer join customer1_ on brand0_.customer_id = customer1_.id where brand0_.id =?
选择品牌品牌0_,其中brands0_.customer_id =?
并且在删除它后:
select * from brand brand0_ left outer join product products1_ on brand0_.id = products1_.brand_id where brand0_.id =?
select * from customer customer0_ left outer join brand1_ on customer0_.id = brands1_.customer_id where customer0_.id =?
它甚至不运行删除查询。我应该怎么做才能删除品牌,并将这些删除流程级联到产品并处理涉及它的产品?我使用CascadeType.All,但它不起作用。
谢谢。
编辑: 我将我的客户模型添加到了问题中。还要注意,当我删除客户实体使用:
customerRepository.delete(id);
它,
从品牌删除,其中id =?
从客户处删除id =?
我将Andy OrfhanRemoval添加到品牌实体中,但没有奏效。也许是因为品牌实体也有一个父实体?因为当我使用相同的方法来删除客户,它只是工作。并且Customer实体没有父级。
指定 orphanRemoval = true
在JPA 2.0中(Hibernate CascadeType.DELETE_ORPHAN
)告诉JPA删除父记录时的子记录。
你能更新你的@OneToMany映射来使用这个属性并尝试吗?对于例如对于品牌来说,它看起来像
@Entity
@Table(name =brand)
public class Brand实现Serializable {
@ManyToOne
@JoinColumn(name =customer_id)
private客户客户;
@OneToMany(mappedBy =brand,cascade = CascadeType.ALL,orphanRemoval = true)
private Collection< Product>产品;
}
It's always been trouble handling cascades in JPA or Hibernate but i really didn't get it now.
I want to delete a parent row brand and childs that are referencing to it. I used CascadeType.ALL but no luck.
Here are my models (Transact <- TransactProduct -> Product -> Brand -> Customer), I'm showing only relations for clarity:
@Entity
@Table(name = "customer")
public class Customer {
@OneToMany(fetch = FetchType.EAGER, mappedBy = "customer", cascade = CascadeType.ALL)
private Collection<Brand> brands;
}
@Entity
@Table(name = "brand")
public class Brand implements Serializable {
@ManyToOne
@JoinColumn(name = "customer_id")
private Customer customer;
@OneToMany(mappedBy = "brand", cascade = CascadeType.ALL)
private Collection<Product> products;
}
@Entity
@Table(name = "product")
public class Product {
@ManyToOne
@JoinColumn(name = "brand_id")
private Brand brand;
@OneToMany(mappedBy = "product", cascade = CascadeType.ALL)
private Collection<TransactProduct> transactProducts;
}
@Entity
@Table(name = "transact")
public class Transact {
@OneToMany(mappedBy = "transact", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Collection<TransactProduct> transactProducts;
@ManyToOne
@JoinColumn(name = "user_id")
private User user;
}
@Entity
@Table(name = "transact_product")
public class TransactProduct {
@ManyToOne
@JoinColumn(name = "product_id")
private Product product;
@ManyToOne
@JoinColumn(name = "transact_id");
private Transact transact;
}
Brand Repository :
@Repository
public interface BrandRepository extends JpaRepository<Brand, Long> {}
In my controller I want to delete a brand like this :
Brand brand = brandRepository.findOne(id);
brandRepository.delete(brand);
After findOne it writes those to console :
select * from brand brand0_ left outer join customer customer1_ on brand0_.customer_id=customer1_.id where brand0_.id=?
select * from brand brands0_ where brands0_.customer_id=?
And after delete it is :
select * from brand brand0_ left outer join product products1_ on brand0_.id=products1_.brand_id where brand0_.id=?
select * from customer customer0_ left outer join brand brands1_ on customer0_.id=brands1_.customer_id where customer0_.id=?
It doesn't even run a delete query. What should I do to delete brands and cascade those delete process to products and transact products that reference to it? I am using CascadeType.All but it doesn't work.
Thanks.
EDIT : I added my customer model to question. And also want to note that when i delete customer entity using :
customerRepository.delete(id);
it does,
delete from brand where id=?
delete from customer where id=?
I added orphanRemoval to Brand entity as Andy Dufresne mentioned but it didn't work. Maybe it's because Brand entity also has a parent entity? Because when I use same method for removing Customers it simply works. And Customer entity doesn't have parent.
Specifying orphanRemoval=true
in JPA 2.0 (Hibernate CascadeType.DELETE_ORPHAN
) tells JPA to remove the child records when the parent is deleted.
Can you update your @OneToMany mappings to use this attribute and try? For e.g. for Brand it would look like
@Entity
@Table(name = "brand")
public class Brand implements Serializable {
@ManyToOne
@JoinColumn(name = "customer_id")
private Customer customer;
@OneToMany(mappedBy = "brand", cascade = CascadeType.ALL, orphanRemoval=true)
private Collection<Product> products;
}
这篇关于JPA CascadeType.All不会删除父行和子行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!