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

后端 未结 6 1009

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 06:07

    You need to ensure both side of the relationship are properly maintained in your code.

    Update Reservation as below and then add the corresponding methods to Product.

    @Entity
    @Table(name = "RESERVATION")
    public class Reservation {
    
        private int reservationId;
    
        private Set products = new HashSet(0);
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        public int getReservationId() {
            return reservationId;
        }
    
        public void setReservationId(int reservationId) {
            this.reservationId = reservationId;
        }
    
        @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
        @JoinTable(name = "product_reservation", joinColumns = { @JoinColumn(name = "reservationId", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "productId", 
                nullable = false, updatable = false) })
        public Set getProducts() {
    
            //force clients through our add and remove methods
            return Collections.unmodifiableSet(products);
        }
    
        public void addProduct(Product product){
    
            //avoid circular calls : assumes equals and hashcode implemented
            if(! products.contains(product){
                products.add(product);
    
                //add method to Product : sets 'other side' of association
                product.addReservation(this);
            }
        }
    
        public void removeProduct(Product product){
    
            //avoid circular calls: assumes equals and hashcode implemented: 
            if(product.contains(product){
                products.remove(product);
    
                //add method to Product: set 'other side' of association: 
                product.removeReservation(this);
            }
        }
    }
    

    And in Products:

    public void addReservation(Reservation reservation){
    
        //assumes equals and hashcode implemented: avoid circular calls
        if(! reservations.contains(reservation){
            reservations.add(reservation);
    
            //add method to Product : sets 'other side' of association
            reservation.addProduct(this);
        }
    }
    
    public void removeReservation(Reservation reservation){
    
        //assumes equals and hashcode implemented: avoid circular calls
        if(! reservations.contains(reservation){
            reservations.remove(reservation);
    
            //add method to Product : sets 'other side' of association
            reservation.reomveProduct(this);
        }
    }
    

    Now you should be able to call save on either Product or Reservation and everything should work as expected.

提交回复
热议问题