无法将数据保存到复合表通过Spring Data rest json post

时间:2017-03-30 07:53:00

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

我在db中有3个表
培训
- training_id(pk)

user_profile
- profile_id(pk)

-training_profile(复合表)  
- training_id   
- profile_id

我已经在具有profile_id = 44的user_profile表中记录并想要为训练表创建新记录,并且还将此新训练与已存在的id为44的user_profile记录相关联,但是在将发布数据保存到训练表之后但它没有插入查找表user_training。
我的对象类是   - 培训班

@Entity
@Table(name = "training", schema = "public")
public class Training implements java.io.Serializable {

    @Id @GeneratedValue
    @Column(name = "training_id", unique = true, nullable = false)
    private Long trainingId;


    @ManyToMany(fetch = FetchType.LAZY, mappedBy = "trainings")
    private Set<UserProfile> userProfiles = new HashSet<UserProfile>(0);

    @Column(name = "training_subject", length = 200)
    private String trainingSubject;

    public Training() {
    }

    public Long getTrainingId() {
        return this.trainingId;
    }

    public void setTrainingId(Long trainingId) {
        this.trainingId = trainingId;
    }


    public String getTrainingSubject() {
        return this.trainingSubject;
    }

    public void setTrainingSubject(String trainingSubject) {
        this.trainingSubject = trainingSubject;
    }


    public Set<UserProfile> getUserProfiles() {
        return this.userProfiles;
    }

    public void setUserProfiles(Set<UserProfile> userProfiles) {
        this.userProfiles = userProfiles;
    }
}
  • 用户配置

    @Entity @Table(name =&#34; user_profile&#34;,schema =&#34; public&#34;)
    公共类UserProfile实现java.io.Serializable {

    @Id @GeneratedValue
    @Column(name = "profile_id", unique = true, nullable = false)
    private Long profileId;
    
    @Column(name = "profile_description")
    private String profileDescription;
    
    @ManyToMany(fetch = FetchType.EAGER, cascade = { CascadeType.ALL })
    @JoinTable(name = "user_training", schema = "public", joinColumns = {
            @JoinColumn(name = "profile_id", nullable = false, updatable = false) }, inverseJoinColumns = {
                    @JoinColumn(name = "training_id", nullable = false, updatable = false) })
    private Set<Training> trainings = new HashSet<Training>(0);
    
    public UserProfile() {
    }
    
    
    public String getProfileDescription() {
        return this.profileDescription;
    }
    
    public void setProfileDescription(String profileDescription) {
        this.profileDescription = profileDescription;
    }
    
    
    public Set<Training> getTrainings() {
        return this.trainings;
    }
    
    public void setTrainings(Set<Training> trainings) {
        this.trainings = trainings;
    }
    

    }

我的json帖子通过邮递员 enter image description here

我得到的回应

enter image description here

响应显示新训练记录插入到training_id为67的表中 没有找到关于此新保存培训的关联 enter image description here

它再次创建了新的培训记录,并且不与现有的用户配置文件相关联,我发布了curl -i -X POST -H&#34; Content-Type:application / json&#34; -d&#34; {\&#34; trainingSubject \&#34; :\&#34; Oracle \&#34;,\&#34; userProfiles \&#34;:[\&#34; / userProfiles / 44 \&#34;]}&#34; http://localhost:8080/api/trainings

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以使用相对网址分配:

{
    "trainingSubject": "oracle",
    "userProfiles":["/userProfiles/44"]
}

也许还可以尝试使用完整的网址:http://localhost:8080/api/userProfiles/44

<强> EDITED

如果您将ManyToMany关系的拥有网站移至Training,则可以使用上述JSON。因此,目前所有者可以设置现实。如果你这样做:

@ManyToMany
@JoinTable(name = "user_training"
, joinColumns = {@JoinColumn(name = "profile_id") }
, inverseJoinColumns = {@JoinColumn(name = "training_id") })
private List<UserProfile> userProfiles = new ArrayList<>();

@ManyToMany(mappedBy = "userProfiles")
private List<Training> trainings = new ArrayList<>();

Training拥有userProfiles内的关系。

我认为在你的情况下,它是目前最好的选择。另一种选择是,将所有者网站保留在UserProfiletransactions,以更新其关系:

PATCH http://localhost:8080/api/userProfiles/44
{
    "trainings": ["trainings/66", "trainings/67"]
}

但是有了这个,你需要多次休息(1。POST新培训并获得新的Id 2.获取当前的培训清单3. PATCH培训列表和新增的培训)

最后一个选项是自己添加REST控制器。

第一种方法的完整文件:

@Entity
@Table
public class Training implements Serializable {

    @Id
    @GeneratedValue
    private Long trainingId;

    @ManyToMany
    @JoinTable(name = "user_training"
    , joinColumns = {@JoinColumn(name = "profile_id") }
    , inverseJoinColumns = {@JoinColumn(name = "training_id") })
    private List<UserProfile> userProfiles = new ArrayList<>();

    @Column(name = "training_subject", length = 200)
    private String trainingSubject;


@Entity
@Table
public class UserProfile implements Serializable {

    @Id
    @GeneratedValue
    private Long profileId;

    @Column(name = "profile_description")
    private String profileDescription;

    @ManyToMany(mappedBy = "userProfiles")
    private List<Training> trainings = new ArrayList<>();


public interface TrainingRepository extends JpaRepository<Training, Long> {
}

public interface UserProfileRepository extends JpaRepository<UserProfile, Long> {
}

使用上面的JSON,这将起作用,我测试了它。您不会直接在curl-POST的响应中看到正确的结果。要查看添加的关系,您必须遵循userProfiles - GET http://localhost:8080/transactions/<newId>/userProfiles

之类的链接