Spring Data 休息如何在@manytomany 关系、具有额外列的复合表上执行 CRUD [英] Spring Data rest how to perform CRUD on @manytomany relation ,composite table with extra column

查看:17
本文介绍了Spring Data 休息如何在@manytomany 关系、具有额外列的复合表上执行 CRUD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法通过来自 Restful 客户端 Postman 的 json POST 在具有额外列的复合表上执行 CRUD.我正在使用 Spring boot、spring data rest 和 spring JPA.我在数据库中有 3 个表
-用户
-能力
-user_competency(带有额外列的连接/复合表)

这是我的课程

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 have 3 tables in data base
-user
-competency
-user_competency (join/composite table with extra column)

Here are my classes

用户

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

用户能力

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

用户能力 ID

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

假设我已经在 User 和 Competency 表中进行了记录,并且我想将我尝试像这样发布的两者都关联起来,但是它给了我 405 Method Not Allowed 的错误.

Suppose i have already record in User and Competency tables and i want to assocaite both i am trying to post like this ,but it give me error of 405 Method Not Allowed.

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

help required ,what should be structure of json to be posted User will already exist and competency will might exist or new can be added and associated with existing user.

推荐答案

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

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

这篇关于Spring Data 休息如何在@manytomany 关系、具有额外列的复合表上执行 CRUD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆