在Spring JPA中删除实体 [英] Deleting entity in Spring JPA

查看:81
本文介绍了在Spring JPA中删除实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两个具有@ManyToOne和@OneToMany关系的实体(类别和产品).当我启用(cascade = CascadeType.ALL)时,产品"中的一条记录会拉出以删除一个类别",即BAD.该实体必须做什么,结果是仅删除发生在一个地方(表),而没有级联(相关)删除以供另一参考???我正在使用Spring 5.1.5(不是Spring Boot)谢谢!

There are two entities with relation @ManyToOne and @OneToMany (Categories and Products). When I enabeling (cascade=CascadeType.ALL) one record in Products pulls for deleting one Category, and that is BAD. 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/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

@实体公共类类别{

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

@实体公共类产品{

    @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)公共ResponseEntity deleteById(@PathVariable Long categoryId,@PathVariable Long productId){productService.deleteProductById(productId);返回新的ResponseEntity<>(HttpStatus.OK);}

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

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

}

推荐答案

已解决,我不知道为什么,但是在crudRepository方法中,deleteById(productId)仅适用于CascadeType.ALL,并且您删除了所有(产品和类别记录)在请求/categories/{catId}/products/{productId}中,这很糟糕.我只是删除(产品产品)和此删除产品.在{id}的情况下,只需通过id请求项目并获取它,然后删除即可.

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. 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.

这篇关于在Spring JPA中删除实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆