Spring Data rest how to perform CRUD on @manytomany relation ,composite table with extra column

后端 未结 2 367
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-10 21:03

I am unable to perform CRUD via json POST from restful client Postman on Composite table having extra column .I am using Spring boot ,spring data rest and spring JPA. I hav

相关标签:
2条回答
  • 2020-12-10 21:27

    Apparently there seems to be no "natural/easy" way to get what you want. But there is a promissing project for integrating embeddables by extending the serialization process: https://github.com/gregturn/embeddable-spring-data-rest

    • UserCompetencyIdJacksonModule, UserCompetencyIdSerializer, ..

    Then you should be able PATCH (not POST) your JSON from above.

    0 讨论(0)
  • 2020-12-10 21:41

    With this code I was able to post a new relation:

    UserCompetency.class

    @Entity
    @Table(name = "user_competency")
    @IdClass(UserCompetencyId.class)
    public class UserCompetency implements java.io.Serializable {
    
        @Id @ManyToOne
        @JoinColumn(name = "competency_id", nullable = false, insertable = false, updatable = false)
        private Competency competency;
    
        @Id @ManyToOne
        @JoinColumn(name = "user_id", nullable = false, insertable = false, updatable = false)
        private User user;
    

    UserCompetencyId.class

    public class UserCompetencyId implements java.io.Serializable {
    
        private Long competency;
    
        private Long user;
    
        public UserCompetencyId() {
        }
    
        public UserCompetencyId(Long competency, Long user) {
            this.competency = competency;
            this.user = user;
        }
    

    UserCompetencyRepository.class

    public interface UserCompetencyRepository extends JpaRepository<UserCompetency, UserCompetencyId> {
    
    }
    

    POST http://localhost:8080/userCompetencies

    {
        "competency": "/competencies/2"
        , "user": "/user/4"
    }
    
    0 讨论(0)
提交回复
热议问题