简体   繁体   English

在 Spring JPA 中删除实体

[英]Deleting entity in Spring JPA

There are two entities with relation @ManyToOne and @OneToMany (Categories and Products).有两个实体具有关系@ManyToOne 和@OneToMany(类别和产品)。 When I enabeling (cascade=CascadeType.ALL) one record in Products pulls for deleting one Category, and that is BAD.当我启用 (cascade=CascadeType.ALL) 产品中的一条记录以删除一个类别时,这很糟糕。 What must be do for this entities that result is only deleting occurs in one place(Table) without cascade(related) delete for another reference???对于这个结果只删除发生在一个地方(表)而没有级联(相关)删除以供另一个参考的实体,必须做什么??? I am using Spring 5.1.5 (not Spring Boot) Thank you!我使用的是 Spring 5.1.5(不是 Spring Boot)谢谢!

SPRING 5 / TOMCAT 9 / JACKSON-DATABIND / spring-data-jpa 2.1.5 / persistence-api 1.0.2 / Hibernate-core 5.4.1 SPRING 5 / TOMCAT 9 / JACKSON-DATABIND / spring-data-jpa 2.1.5 / persistence-api 1.0.2 / Hibernate-core 5.4.1

@Entity public class Category { @Entity 公共类类别{

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="CAT_ID")
private Long id;

@Column(name="CAT_NAME")
private String name;

@JsonManagedReference
@OneToMany(cascade=CascadeType.ALL,fetch = FetchType.EAGER)
@JoinColumn(name="CAT_ID")
@OrderBy
private Set<Product> products = new HashSet<>();

public Long getId() {
    return id;
}

@Entity public class Product { @Entity 公共类产品{

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="PRODUCT_ID")
    private Long id;

    @Column(name="PRODUCT_NAME")
    private String name;

    @JsonBackReference
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name="CAT_ID")
    private Category category;

    public Long getId() {
        return id;
    }

@RequestMapping(value="/categories/{categoryId}/products/{productId}", method=RequestMethod.DELETE) public ResponseEntity deleteById(@PathVariable Long categoryId, @PathVariable Long productId) { productService.deleteProductById(productId); @RequestMapping(value="/categories/{categoryId}/products/{productId}", method=RequestMethod.DELETE) public ResponseEntity deleteById(@PathVariable Long categoryId, @PathVariable Long productId) { productService.deleteProductById(productId); return new ResponseEntity<>(HttpStatus.OK);返回新的 ResponseEntity<>(HttpStatus.OK); } }

@Transactional
@Override
public void deleteProductById(Long productId) {
    // TODO Auto-generated method stub
    productRepository.deleteById(productId);

}

SOLVED, I don't know why, but in crudRepository method deleteById(productId) worked only with CascadeType.ALL and you delete all(product and category records) in the request /categories/{catId}/products/{productId} and this bad.已解决,我不知道为什么,但是在 crudRepository 方法中 deleteById(productId) 仅与 CascadeType.ALL 一起使用,并且您删除了请求 /categories/{catId}/products/{productId} 中的所有(产品和类别记录)和这个坏的。 I am usung just delete(Product product) and this delete product.我只是删除(产品产品)和这个删除产品。 In the case of {id} just do request project by id and got it, next delete.在 {id} 的情况下,只需按 id 请求项目并得到它,然后删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM