0

I started to learn spring data rest. I'm doing PUT operation and it's not working for the nested objects for ManyToMany relationship, whereas it works fine for OneToMany relation.

Entities structures:

@Table(name="CONFIG_DTLS",schema = "app_txn")
@Entity
public class Config {

    @Id
    @GenericGenerator(name = "UUIDGenerator", strategy = "uuid2")
    @GeneratedValue(generator = "UUIDGenerator")
    @Column(name = "ID", updatable = false, nullable = false)
    private UUID id;


    @Column(name = "NAME", nullable = false, length = 75)
    private String name;
    
    
    /*Unable to replace the data in the MBR_CONFIG_MAPPING table in the put operation. 
      When the control comes to @HandleBeforeSave annotated method in PUT operation,
      the request data contains the existing Member info instead of the one which i'm passing in the PUT request body */
    @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE},fetch = FetchType.EAGER)
    @JoinTable(schema = "app_txn", name = "MBR_CONFIG_MAPPING",
                joinColumns ={@JoinColumn(name="CONFIG_ID",referencedColumnName = "ID")}, 
                inverseJoinColumns = {@JoinColumn(name="MBR_ID",referencedColumnName = "ID")}
    )
    private Set<Member> members;

    //able to replace the notifications completely in PUT operation
    @OneToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
    @JoinColumn(name = "CONFIG_ID",referencedColumnName = "ID")
    private Set<Notification> notifications; 
}

Member.java

@Table(name="MBR_DTLS",schema = "app_txn")
@Entity
public class Member {

    @Id
    @GenericGenerator(name = "UUIDGenerator", strategy = "uuid2")
    @GeneratedValue(generator = "UUIDGenerator")
    @Column(name = "ID", updatable = false, nullable = false)
    private UUID id;
    
    @Column(name = "OTHER_MBR_DATA", updatable = false)
    private String otherMbrData;
}

Notification.java

@Table(name="NOTIFICATIONS",schema = "app_txn")
@Entity
public class Notification {
    
    @Id
    @GenericGenerator(name = "UUIDGenerator", strategy = "uuid2")
    @GeneratedValue(generator = "UUIDGenerator")
    @Column(name = "ID", updatable = false, nullable = false)
    private UUID id;
    
    @Column(name="LEVEL")
    private String level;

    @Column(name="EMAIL")
        private String email;
        

}

Interfaces:

@RepositoryRestResource(collectionResourceRel = "configs", path="configs")
public interface ConfigRepo extends PagingAndSortingRepository<Config,UUID> {    
  
}



@RepositoryRestResource(exported=false) // don't want to users to manipulate it directly.
public interface MemberRepo extends PagingAndSortingRepository<Member,Object> {
   

}

Here I don't want to add or modify anything in the MBR_DTLS table as it is loaded by another backend process. I want to update only the mapping details MBR_CONFIG_MAPPING table whenever user does the PUT/update operation. POST/create operation is working fine. Please share your thoughts on how to fix this and if you have any questions add it in the comment section.

PS: I referred some links online but that does not help much - Spring Data REST - PUT request does not work properly since v.2.5.7

Parthiban
  • 43
  • 1
  • 11

0 Answers0