1
votes

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

@Entity public class Category {

@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 {

    @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); return new ResponseEntity<>(HttpStatus.OK); }

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

}
1
Im not sure I understand what you are trying to achieve? Are you trying to deleted both linked entities without using (cascade=CascadeType.ALL)?Mark Brown
Well, don't set cascade=CascadeType.ALL on Product.category, since that is precisely what deletes a category when deleting a product, and you don't want that to happen. The code you posted won't delete a category when deleting a product, so I'm not sure what your question is.JB Nizet
When i send DELETE localhost:8080/spring-mvc-app/categories/1/products/1 , i want to delete product 1(one) not(No) category1(ONE)Neumann
And your code should do that. What does it actually do? What's the concrete problem with the code you posted?JB Nizet
localhost:8080/spring-mvc-app/categories/1/products/1 - there no affect. But when i add cascade=CascadeType.ALL for Product entity, I got detele add product and related category. I just want delete product only no category. How to do that?Neumann

1 Answers

0
votes

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.