本文介绍了无法通过 Spring Data rest json post 将数据保存到复合表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我在数据库中有 3 个表培训 - training_id (pk)user_profile - profile_id (pk)-training_profile(复合表) - training_id - profile_id我已经在 user_profile 表中记录了 profile_id=44 并想为训练表创建新记录,并将此新训练与 ID 为 44 的现有 user_profile 记录相关联,但在将发布数据保存到训练表后但它没有插入到查找表 user_training 中.我的项目等级是- 培训班@Entity@Table(name = "training", schema = "public")公共类培训实现 java.io.Serializable {@Id @GeneratedValue@Column(name = "training_id", unique = true, nullable = false)私人长期培训ID;@ManyToMany(fetch = FetchType.LAZY,mappedBy = "trainings")私有集userProfiles = new HashSet(0);@Column(name = "training_subject", 长度 = 200)私人字符串培训主题;公共培训(){}公共长 getTrainingId() {返回 this.trainingId;}public void setTrainingId(Long trainingId) {this.trainingId = trainingId;}公共字符串 getTrainingSubject() {返回 this.trainingSubject;}public void setTrainingSubject(String trainingSubject) {this.trainingSubject = trainingSubject;}公共集获取用户配置文件(){返回 this.userProfiles;}public void setUserProfiles(Set userProfiles) {this.userProfiles = userProfiles;}}用户资料@Entity@Table(name = "user_profile", schema = "public")公共类 UserProfile 实现 java.io.Serializable {@Id @GeneratedValue@Column(name = "profile_id", unique = true, nullable = false)私人长档案ID;@Column(name = "profile_description")私人字符串配置文件描述;@ManyToMany(fetch = FetchType.EAGER, 级联 = { 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) })私人设置<培训>训练 = 新 HashSet(0);公共用户配置文件(){}公共字符串 getProfileDescription() {返回 this.profileDescription;}public void setProfileDescription(String profileDescription) {this.profileDescription = profileDescription;}公共集获取培训(){返回 this.trainings;}public void setTrainings(Set trainings) {this.trainings = 培训;}}我通过邮递员的 json 帖子我得到的回应响应显示新的训练记录插入表中,training_id 为 67未找到此新保存的培训的关联它再次为训练创建了新记录并且不与现有用户配置文件关联,我发布 curl -i -X POST -H "Content-Type:application/json" -d "{ \"trainingSubject\" : \"Oracle\", \"userProfiles\":[\"/userProfiles/44\"] }" 解决方案 您可以使用相对 url 分配:{"trainingSubject": "oracle","userProfiles":["/userProfiles/44"]}也许也可以尝试使用完整网址:http://localhost:8080/api/userProfiles/44已编辑如果您将 ManyToMany 关系的拥有站点移动到 Training,它将使用上述 JSON.因此,目前允许所有者设置房地产.如果你这样做:@ManyToMany@JoinTable(name = "user_training", joinColumns = {@JoinColumn(name = "profile_id") }, inverseJoinColumns = {@JoinColumn(name = "training_id") })私人列表<UserProfile>userProfiles = new ArrayList();加上@ManyToMany(mappedBy = "userProfiles")私人名单<培训>培训 = 新的 ArrayList();Training 拥有 userProfiles 内的关系.我认为就您而言,这是目前最好的选择.另一种选择是,当在 transactions 上将所有者站点保持在 UserProfile 时,更新那里的关系,如:PATCH http://localhost:8080/api/userProfiles/44{培训":[培训/66",培训/67"]}但是有了这个,您将需要多次休息调用(1. 发布新训练并获取新 Id 2. 获取当前训练列表 3. 带有新添加训练的 PATCH 训练列表)最后一个选项是自己添加 REST 控制器.第一种方法的完整文件:@Entity@表公共类培训实现可序列化{@Id@GeneratedValue私人长期培训ID;@ManyToMany@JoinTable(name = "user_training", joinColumns = {@JoinColumn(name = "profile_id") }, inverseJoinColumns = {@JoinColumn(name = "training_id") })私人列表<UserProfile>userProfiles = new ArrayList();@Column(name = "training_subject", 长度 = 200)私人字符串培训主题;@实体@表公共类 UserProfile 实现了 Serializable {@Id@GeneratedValue私人长档案ID;@Column(name = "profile_description")私人字符串配置文件描述;@ManyToMany(mappedBy = "userProfiles")私人名单<培训>培训 = 新的 ArrayList();公共接口 TrainingRepository 扩展 JpaRepository{}公共接口 UserProfileRepository 扩展 JpaRepository{}使用上层 JSON 这将起作用,我对其进行了测试.您不会在 curl-POST 的响应中直接看到正确的结果.要查看添加的关系,您必须遵循 userProfiles 链接,如 GET http://localhost:8080/transactions/<newId>/userProfilesI have 3 Tables in dbtraining - training_id (pk)user_profile - profile_id (pk)-training_profile (composite table) - training_id - profile_idI have already record in user_profile table having profile_id=44 and want to create new record for training table ,and also to associate this new training with already existing user_profile record which has id 44,but after post data is saved to training table but it is not inserted into lookup table user_training. My Object Classes Are - Training Class@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; }}UserProfile @Entity@Table(name = "user_profile", schema = "public")public class UserProfile implements 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;}}My json post via postmanAnd Response I getResponse show that new training record inserted in table having training_id as 67 No association found for this new saved trainingagain it created new record for training and does not associate with existing user profile , I post curl -i -X POST -H "Content-Type:application/json" -d "{ \"trainingSubject\" : \"Oracle\", \"userProfiles\":[\"/userProfiles/44\"] }" http://localhost:8080/api/trainings 解决方案 You could use the relative url assignment:{ "trainingSubject": "oracle", "userProfiles":["/userProfiles/44"]}Maybe also try with the full url: http://localhost:8080/api/userProfiles/44EDITEDIf you move the owning site of the ManyToMany relation to Training it will work with the above JSON. So currently the owner is allowed to set the realtions. If you do it like that:@ManyToMany@JoinTable(name = "user_training", joinColumns = {@JoinColumn(name = "profile_id") }, inverseJoinColumns = {@JoinColumn(name = "training_id") })private List<UserProfile> userProfiles = new ArrayList<>();plus@ManyToMany(mappedBy = "userProfiles")private List<Training> trainings = new ArrayList<>();Training owns the relation within userProfiles.I think in your case it's the best option for now. Another option would be, when keeping the owner site at UserProfile on transactions, to update the relation there like: PATCH http://localhost:8080/api/userProfiles/44{ "trainings": ["trainings/66", "trainings/67"]}But with this you would need multible rest calls (1. POST new training and get the new Id 2. GET current training list 3. PATCH trainings list with newly added training)Last option would be to add the REST-controller on your own.Complete files for the first approach:@Entity@Tablepublic 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@Tablepublic 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> {}With the upper JSON this will work, I tested it. You will not see the correct result directly in the response of curl-POST. To see the added relation you must follow the userProfiles-link like GET http://localhost:8080/transactions/<newId>/userProfiles 这篇关于无法通过 Spring Data rest json post 将数据保存到复合表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-23 08:42