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

天大地大妈咪最大 提交于 2019-11-28 12:59:17

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"
}

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!