Spring boot Spring数据rest如何在@manytomany关系上执行CRUD,具有额外列的复合表

Spring boot Spring数据rest如何在@manytomany关系上执行CRUD,具有额外列的复合表,spring-boot,spring-data-jpa,spring-data-rest,Spring Boot,Spring Data Jpa,Spring Data Rest,我无法通过json POST在具有额外列的复合表上从restful客户端Postman执行CRUD。我使用的是Spring boot、Spring data rest和Spring JPA。 我在数据库中有3个表 -使用者 -能力 -用户能力(带额外列的联接/组合表) 这是我的课 用户 @Entity @Table(name = "\"user\"", schema = "public") @JsonIdentityInfo( generator = ObjectIdGen

我无法通过json POST在具有额外列的复合表上从restful客户端Postman执行CRUD。我使用的是Spring boot、Spring data rest和Spring JPA。 我在数据库中有3个表
-使用者
-能力
-用户能力(带额外列的联接/组合表)

这是我的课

用户

@Entity
@Table(name = "\"user\"", schema = "public")
@JsonIdentityInfo(
          generator = ObjectIdGenerators.IntSequenceGenerator.class, 
          property = "userId")
public class User implements java.io.Serializable {

    private Long userId;

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)

    @Column(name = "user_id", unique = true, nullable = false)
    public Long getUserId() {
        return this.userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    private Set<UserCompetency> userCompetencies = new HashSet<UserCompetency>(0);

        @OneToMany(fetch = FetchType.EAGER,cascade = {CascadeType.ALL}, mappedBy = "user")
    public Set<UserCompetency> getUserCompetencies() {
        return this.userCompetencies;
    }

    public void setUserCompetencies(Set<UserCompetency> userCompetencies) {
        this.userCompetencies = userCompetencies;
    }

}
UserCompetencyId

@Entity
@Table(name = "user_competency", schema = "public")
@JsonIdentityInfo(
          generator =ObjectIdGenerators.IntSequenceGenerator.class, 
          property = "id")
public class UserCompetency implements java.io.Serializable {
    private UserCompetencyId id;
    private Level level;
    private User user;
    private Competency competency;

    @EmbeddedId

    @AttributeOverrides({
            @AttributeOverride(name = "competencyId", column = @Column(name = "competency_id", nullable = false)),
            @AttributeOverride(name = "userId", column = @Column(name = "user_id", nullable = false)) })
    public UserCompetencyId getId() {
        return this.id;
    }

    public void setId(UserCompetencyId id) {
        this.id = id;
    }

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "level_id")
    public Level getLevel() {
        return this.level;
    }

    public void setLevel(Level level) {
        this.level = level;
    }

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "user_id", nullable = false, insertable = false, updatable = false)
    public User getUser() {
        return this.user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @ManyToOne(fetch = FetchType.EAGER,cascade=CascadeType.ALL)
    @JoinColumn(name = "competency_id", nullable = false, insertable = false, updatable = false)
    public Competency getCompetency() {
        return this.competency;
    }

    public void setCompetency(Competency competency) {
        this.competency = competency;
    }
}   
@Embeddable
public class UserCompetencyId implements java.io.Serializable {

    private Long competencyId;
    private Long userId;

    public UserCompetencyId() {
    }

    public UserCompetencyId(Long competencyId, Long userId) {
        this.competencyId = competencyId;
        this.userId = userId;
    }


    @Column(name = "competency_id", nullable = false)
    public Long getCompetencyId() {
        return this.competencyId;
    }

    public void setCompetencyId(Long competencyId) {
        this.competencyId = competencyId;
    }

    @Column(name = "user_id", nullable = false)
    public Long getUserId() {
        return this.userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public boolean equals(Object other) {
        if ((this == other))
            return true;
        if ((other == null))
            return false;
        if (!(other instanceof UserCompetencyId))
            return false;
        UserCompetencyId castOther = (UserCompetencyId) other;

        return (this.getCompetencyId() == castOther.getCompetencyId()) && (this.getUserId() == castOther.getUserId());
    }    
}
public interface UserCompetencyRepository extends JpaRepository<UserCompetency, UserCompetencyId> {

}
假设我已经记录在用户和能力表中,并且我想将这两者关联起来,我正试图这样发布,但它给了我405方法不允许的错误。


需要帮助,发布给用户的json结构应该是什么?用户将已经存在,并且可能存在能力,或者可以添加新的能力,并与现有用户关联

显然,似乎没有“自然/简单”的方式来获得你想要的东西。但是有一个很有希望的项目是通过扩展序列化过程来集成嵌入式:

  • usercompetincyidjacksonmodule
    usercompetincyidSerializer

然后,您应该能够从上面修补(而不是发布)您的JSON。

通过这段代码,我能够发布一个新的关系:

usercapability.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;
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;
    }
UserCompetencyId.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;
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

@Entity
@Table(name = "user_competency", schema = "public")
@JsonIdentityInfo(
          generator =ObjectIdGenerators.IntSequenceGenerator.class, 
          property = "id")
public class UserCompetency implements java.io.Serializable {
    private UserCompetencyId id;
    private Level level;
    private User user;
    private Competency competency;

    @EmbeddedId

    @AttributeOverrides({
            @AttributeOverride(name = "competencyId", column = @Column(name = "competency_id", nullable = false)),
            @AttributeOverride(name = "userId", column = @Column(name = "user_id", nullable = false)) })
    public UserCompetencyId getId() {
        return this.id;
    }

    public void setId(UserCompetencyId id) {
        this.id = id;
    }

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "level_id")
    public Level getLevel() {
        return this.level;
    }

    public void setLevel(Level level) {
        this.level = level;
    }

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "user_id", nullable = false, insertable = false, updatable = false)
    public User getUser() {
        return this.user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @ManyToOne(fetch = FetchType.EAGER,cascade=CascadeType.ALL)
    @JoinColumn(name = "competency_id", nullable = false, insertable = false, updatable = false)
    public Competency getCompetency() {
        return this.competency;
    }

    public void setCompetency(Competency competency) {
        this.competency = competency;
    }
}   
@Embeddable
public class UserCompetencyId implements java.io.Serializable {

    private Long competencyId;
    private Long userId;

    public UserCompetencyId() {
    }

    public UserCompetencyId(Long competencyId, Long userId) {
        this.competencyId = competencyId;
        this.userId = userId;
    }


    @Column(name = "competency_id", nullable = false)
    public Long getCompetencyId() {
        return this.competencyId;
    }

    public void setCompetencyId(Long competencyId) {
        this.competencyId = competencyId;
    }

    @Column(name = "user_id", nullable = false)
    public Long getUserId() {
        return this.userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public boolean equals(Object other) {
        if ((this == other))
            return true;
        if ((other == null))
            return false;
        if (!(other instanceof UserCompetencyId))
            return false;
        UserCompetencyId castOther = (UserCompetencyId) other;

        return (this.getCompetencyId() == castOther.getCompetencyId()) && (this.getUserId() == castOther.getUserId());
    }    
}
public interface UserCompetencyRepository extends JpaRepository<UserCompetency, UserCompetencyId> {

}

我下载了这个项目并尝试复制相同的更改,当我在主类上使用@Import(RepositoryRestMvcConfiguration.class)时,我遇到了一个问题,api端点不可访问,当我访问它时,会出现404未找到错误我认为不需要RepositoryRestMvcConfiguration。但是其他的东西也帮不了你。所以我添加了一个新的答案。。我们上周遇到的另一个问题。。在哪里可以解决它?谢谢。我通过实现自定义控制器函数解决了这个问题。它解决了数据保存到复合表中的问题,但我的UserCapability类还有一个属性,所以我在表中有另一列,即level_id,这也是外键,在顶部屏幕截图中给出,请看一下,现在我应该如何在json post private UserCompetencyId中发布级别id;私人层面;私人用户;私人能力;添加
,“level”:“/level/123”
?明白了,实际上存储库类并没有针对level和面临的问题公开,我还需要问的一件事是,如果我有一个现有用户,我想添加新的能力,以便它保存到能力表中,并通过json post将其与1中的现有用户关联,我将如何做。也将是一个自定义控制器。或者两个休息电话。一个用于获取
能力
的新Id,然后一个用于发布新的
用户能力
关系。如何对用户能力、组合表执行更新和删除