Spring data JPA and hibernate detached entity passed to persist on ManyToMany relationship

后端 未结 6 1013

I am trying to persist an object that has a many-to-many relationship with other objects already persisted.

Here is my persisted object (they are already persisted i

6条回答
  •  天命终不由人
    2020-11-30 05:59

    The exception comes as hibernate trying to persist associated products when you save reservation. Persisting the products is only success if they have no id because id of Product is annotated

    @GeneratedValue(strategy=GenerationType.AUTO)
    

    But you got products from repository and ids are not null.

    There 2 options to resolve your issue:

    1. remove (cascade = CascadeType.ALL) on products of Reservation
    2. or remove @GeneratedValue(strategy=GenerationType.AUTO) on id of Product

提交回复
热议问题