java - 从父项中删除子项的 Spring Boot 问题

标签 java spring hibernate spring-mvc jpa

希望得到大家的建议。我很喜欢 Spring boot 框架,但在使用 JPA 和 Hibernate 从祖 parent 中删除 parent 时遇到问题。

请注意,我的设置相当复杂,因此我将快速解释一下布局:

child (网站)可以拥有:

  • 1 个产品(父级)
  • 1 个提供商
  • 许多价格( child )

父级(产品)可以有:

  • 许多网站( child )

提供者可以有:

  • 许多网站

价格可以有:

  • 1 个网站(父网站)

所以这就像祖 parent -> parent -> child 的关系。我的问题是,如何在不删除祖 parent 和提供者的情况下删除 parent ?

这是我的类(class)布局:

祖 parent :

@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;
}

child :

@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;
}

Controller :

@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();
}

删除是此存储库的默认方法,但它不会删除网站。以下是当我点击删除 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=?

我希望这是有道理的

最佳答案

我想说没有 Activity 事务,没有事务 JPA 不会删除任何内容。

尝试将@Transactional添加到 Controller 中的deletePost。

关于java - 从父项中删除子项的 Spring Boot 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46360143/

相关文章:

java: Spring:如何将 bean 从一个 ClassPathXMLApplicationContext 传输到另一个?

hibernate - 多对多关系jpa中的补充属性

java - 无法使用 Spring、Hibernate 和 c3p0 创建 dataSource bin

java - 如何使用 Hibernate 将 Java 对象映射到 H2 中的 clob 和 Postgres 中的 json

mysql - grails 不允许导致数据修改的查询

java - Spring 数据休息 : RepositoryRestController deserialization from URI not working

java - 用Spring实现单例模式的好方法

java android - 迭代数组中的JSON对象

java - 使用适用于 Android 的 XSL-FO 生成 PDF

java - Springboot嵌入式Tomcat类加载器缓慢