我希望得到你們所有人的建議。我很享受Spring引導框架,但是我遇到了使用JPA和Hibernate刪除祖父母的問題。從父母刪除孩子的春季啓動問題
我有一個相當複雜的設置程序提醒你,所以我很快就會解釋佈局:
孩子(網站)可以有:
- 1產品(父)
- 1提供
- 許多價格(兒童)
父(產品)可得:
- 許多網站(兒童)
供應商可以有:
- 許多網站
的價格可以有:
- 1網站(父)
所以它就像一個祖父母 - >父母 - >孩子的關係。我的問題是,如何在不刪除祖父母和提供者的情況下刪除父母?
這裏是我的客艙佈局:
祖父母:
@Entity
@NoArgsConstructor(force = true)
@RequiredArgsConstructor
@Table(name = "product")
@Getter
@Setter
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@OneToMany(mappedBy = "product", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Website> website;
}
家長:
@Entity
@NoArgsConstructor(force = true)
@RequiredArgsConstructor
@Table(name = "website")
@Getter
@Setter
public class Website {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@ManyToOne
@JoinColumn(name = "product_id")
private @NotNull Product product;
@ManyToOne
@JoinColumn(name = "provider_id")
private @NotNull Provider provider;
@OneToMany(mappedBy = "website", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
private List<Price> priceList;
}
提供者:
@Entity
@NoArgsConstructor(force = true)
@RequiredArgsConstructor
@Table(name = "provider")
@Getter
@Setter
public class Provider {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@JsonIgnore
@OneToMany(mappedBy = "provider", fetch = FetchType.EAGER)
private List<Website> website;
}
兒童:
@Entity
@NoArgsConstructor(force = true)
@RequiredArgsConstructor
@Table(name = "price")
@Getter
@Setter
public class Price {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@ManyToOne
@JoinColumn(name = "website_id")
private Website website;
}
控制器:
@Autowired
private IWebsiteRepository repository;
@RequestMapping(path = "/admin/delete/{id}", method = RequestMethod.POST)
public ModelAndView deletePost(@PathVariable("id") long id) {
repository.delete(id);
return new ModelAndView("redirect:/price/api/website/admin/");
}
資源庫:
public interface IWebsiteRepository extends CrudRepository<Website, Long> {
Website findById(long id);
List<Website> findAll();
}
刪除是這裏的默認方法爲這個資料庫,但它不會刪除該網站。這裏是會從休眠輸出,當我按下刪除控制端點:
Hibernate: select website0_.id as id1_4_, website0_.date as date2_4_, website0_.product_id as product_4_4_, website0_.provider_id as provider5_4_, website0_.url as url3_4_ from website website0_
Hibernate: select product0_.id as id1_2_0_, product0_.activate as activate2_2_0_, product0_.date as date3_2_0_, product0_.name as name4_2_0_, website1_.product_id as product_4_4_1_, website1_.id as id1_4_1_, website1_.id as id1_4_2_, website1_.date as date2_4_2_, website1_.product_id as product_4_4_2_, website1_.provider_id as provider5_4_2_, website1_.url as url3_4_2_, pricelist2_.website_id as website_4_1_3_, pricelist2_.id as id1_1_3_, pricelist2_.id as id1_1_4_, pricelist2_.date as date2_1_4_, pricelist2_.price as price3_1_4_, pricelist2_.website_id as website_4_1_4_, provider3_.id as id1_3_5_, provider3_.colour as colour2_3_5_, provider3_.date as date3_3_5_, provider3_.name as name4_3_5_, provider3_.target_name as target_n5_3_5_ from product product0_ left outer join website website1_ on product0_.id=website1_.product_id left outer join price pricelist2_ on website1_.id=pricelist2_.website_id left outer join provider provider3_ on website1_.provider_id=provider3_.id where product0_.id=?
Hibernate: select product0_.id as id1_2_0_, product0_.activate as activate2_2_0_, product0_.date as date3_2_0_, product0_.name as name4_2_0_, website1_.product_id as product_4_4_1_, website1_.id as id1_4_1_, website1_.id as id1_4_2_, website1_.date as date2_4_2_, website1_.product_id as product_4_4_2_, website1_.provider_id as provider5_4_2_, website1_.url as url3_4_2_, pricelist2_.website_id as website_4_1_3_, pricelist2_.id as id1_1_3_, pricelist2_.id as id1_1_4_, pricelist2_.date as date2_1_4_, pricelist2_.price as price3_1_4_, pricelist2_.website_id as website_4_1_4_, provider3_.id as id1_3_5_, provider3_.colour as colour2_3_5_, provider3_.date as date3_3_5_, provider3_.name as name4_3_5_, provider3_.target_name as target_n5_3_5_ from product product0_ left outer join website website1_ on product0_.id=website1_.product_id left outer join price pricelist2_ on website1_.id=pricelist2_.website_id left outer join provider provider3_ on website1_.provider_id=provider3_.id where product0_.id=?
Hibernate: select website0_.provider_id as provider5_4_0_, website0_.id as id1_4_0_, website0_.id as id1_4_1_, website0_.date as date2_4_1_, website0_.product_id as product_4_4_1_, website0_.provider_id as provider5_4_1_, website0_.url as url3_4_1_, product1_.id as id1_2_2_, product1_.activate as activate2_2_2_, product1_.date as date3_2_2_, product1_.name as name4_2_2_ from website website0_ inner join product product1_ on website0_.product_id=product1_.id where website0_.provider_id=?
我希望這是有道理的
您可以啓用更詳細的日誌記錄併發布相關輸出嗎? – pleft