我希望从大家那里得到一些建议。我很喜欢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输出的内容:

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。

10-07 13:04
查看更多