春季启动问题是从父母删除孩子的问题



我希望从大家那里得到一些建议。我喜欢春季靴子框架,但是我在使用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();
}

删除是此存储库的默认方法,但没有删除网站。当我击中Delete Controller端点时,这是从Hibernate输出的原因:

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=?

我希望这有意义

我会说没有主动事务,如果没有交易,jpa不会删除任何内容。

尝试将 @transactional添加到控制器中的deletepost。

最新更新