【问题标题】:Spring boot issue with deleting child from a parent从父级删除子级的 Spring Boot 问题
【发布时间】:2018-03-03 18:18:57
【问题描述】:

我希望从大家那里得到一些建议。我很喜欢 Spring Boot 框架,但在使用 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=?

我希望这是有道理的

【问题讨论】:

  • 您可以启用更详细的日志记录并发布相关输出吗?

标签: java spring hibernate spring-mvc jpa


【解决方案1】:

我会说没有活动的交易,没有交易,JPA 不会删除任何东西。

尝试将@Transactional 添加到控制器中的deletePost。

【讨论】:

  • 您好,我尝试将该注释放在控制器中的 deletePost 方法中,但没有任何效果
  • 能否提供Spring Boot Application类。你有什么样的额外配置?
  • 很少,Spring boot 应用程序类是最基本的。没有配置。
  • 我做了一个demo项目,delete可以正常工作:github.com/simasch/46360538你可以把这个和你的解决方案比较一下。
猜你喜欢
  • 2021-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多