Spring Data休息如何在@manytomany关系上执行CRUD,使用额外列复合表

时间:2017-04-03 14:06:55

标签: spring-boot spring-data-jpa spring-data-rest

我无法通过具有额外列的复合表上的restful client Postman通过json POST执行CRUD。我正在使用Spring启动,spring数据休息和spring JPA。  我在数据库中有3个表
-user
-competency
-user_competency(带有额外列的连接/复合表)
enter image description here
这是我的课程

用户

@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;
    }

}

能力

@Entity
@Table(name = "competency", schema = "public")
@JsonIdentityInfo(
          generator = ObjectIdGenerators.IntSequenceGenerator.class, 
          property = "competencyId")
public class Competency implements java.io.Serializable {


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

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)

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

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

        @OneToMany(fetch = FetchType.LAZY, mappedBy = "competency")
    public Set<UserCompetency> getUserCompetencies() {
        return this.userCompetencies;
    }

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

UserCompetency

@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;
    }
}   

UserCompetencyId

@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());
    }    
}

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

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

2 个答案:

答案 0 :(得分:1)

使用此代码,我能够发布一个新的关系:

<强> 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> {

}

发布 http://localhost:8080/userCompetencies

{
    "competency": "/competencies/2"
    , "user": "/user/4"
}

答案 1 :(得分:0)

显然似乎没有&#34;自然/容易&#34;得到你想要的方式。但是有一个通过扩展序列化过程来整合嵌入式项目的突出项目:https://github.com/gregturn/embeddable-spring-data-rest

  • UserCompetencyIdJacksonModuleUserCompetencyIdSerializer,..

然后你应该可以从上面 PATCH (不是 POST )你的JSON。