大佬教程收集整理的这篇文章主要介绍了Jpa 无法将 json 映射到每次都重新创建的实体,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个包含 3 个实体的项目,如示例中所示:
1:
@Entity
@table(name = "GOAL")
@Getter
@Setter
@EqualsAndHashCode(onlyExplicitlyIncluded = truE)
public class GoalEntity implements Length {
@ID
@GeneratedValue(strategy = GenerationType.IDENTITY)
@column(name = "GOAL_ID",nullable = false,updatable = truE)
@EqualsAndHashCode.Include
private Long ID;
@column(name = "name",length = Name)
private String name;
@OnetoMany(mappedBy = "goal",fetch = FetchType.EAGER,cascade = CascadeType.ALL,orphanRemoval = truE)
private List<GoalQuestionEntity> goalQuestions;
}
2:
@Entity
@table(name = "GOAL_QUESTION")
@Getter
@Setter
@EqualsAndHashCode(onlyExplicitlyIncluded = truE)
public class GoalQuestionEntity implements MprcMaxLength {
@ID
@GeneratedValue(strategy = GenerationType.IDENTITY)
// @GeneratedValue(strategy = GenerationType.SEQUENCE)
@column(name = "GOAL_QUESTION_ID",updatable = falsE)
@EqualsAndHashCode.Include
private Long ID;
@column(name = "title",length = Name)
private String title;
@column(name = "DESCRIPTION",length = Name)
private String description;
@manyToOne(fetch = FetchType.EAGER)
@Joincolumn(name = "GOAL_ID")
private GoalEntity goal;
@OnetoMany(mappedBy = "goalQuestion",orphanRemoval = truE)
private List<GoalQuestionANSWEREntity> goalQuestionAnswers;
}
3:
@Entity
@table(name = "GOAL_QUESTION_ANSWER")
@Getter
@Setter
@EqualsAndHashCode(onlyExplicitlyIncluded = truE)
public class GoalQuestionANSWEREntity implements MprcMaxLength {
@ID
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@column(name = "GOAL_QUESTION_ANSWER_ID",updatable = falsE)
@EqualsAndHashCode.Include
private Long ID;
@column(name = "TEXT",length = Name)
private String text;
@manyToOne(fetch = FetchType.EAGER)
@Joincolumn(name = "GOAL_QUESTION_ID")
private GoalQuestionEntity goalQuestion;
}
还有一个仓库:
@Repository
public interface GoalJpaRepository extends JpaRepository<GoalEntity,Long> {
}
现在,我想在每次传递整个数据结构时从前端调用 API:
[
{
"ID": 25,"name": "goal 1","goalQuestions": [
{
"ID": 21,"title": "title","description": "desc","goalQuestionAnswers": [{
"ID": null,"text": "Hello"
}
]
}
]
},{
"ID": 26,"name": "goal 2","goalQuestions": null
},{
"ID": 27,"name": "goal 3","goalQuestions": null
}
]
如果我传递空或不存在的实体 ID,我想创建一个新实体,如果 ID 已保存,我想更新实体。否则我想删除实体。 可以用 jpa 做到这一点吗?
这是我正在使用的服务中的更新方法:
public List<GoalResponse> update(List<Goalrequest> goalrequestList) {
// goalJpaRepository.deleteall();
List<GoalEntity> goalEntityList = goalrequestList.stream().map(goalrequest -> {
GoalEntity goalEntity = goalJpaRepository.findByID(goalrequest.getID()).orElse(new GoalEntity());
questionsHelper.maprequestToEntity(goalrequest,goalEntity);
return goalEntity;
}).collect(Collectors.toList());
goalEntityList = goalJpaRepository.saveAll(goalEntityList);
return goalEntityList.stream().map(questionsHelper::mapEntityToResponsE).collect(Collectors.toList());
}
这是映射方法:
public GoalEntity maprequestToEntity(Goalrequest goalrequest,GoalEntity goalEntity) {
goalEntity.setID(goalrequest.getID());
goalEntity.setname(goalrequest.getname());
if (CollectionUtils.isnotEmpty(goalrequest.getGoalQuestions())) {
if (goalEntity.getGoalQuestions() == null) {
goalEntity.setGoalQuestions(new linkedList<>());
}
goalEntity.getGoalQuestions().clear();
goalEntity.getGoalQuestions().addAll(mapGoalQuestionsrequestToEntity(goalrequest.getGoalQuestions(),goalEntity));
}
return goalEntity;
}
private List<GoalQuestionEntity> mapGoalQuestionsrequestToEntity(List<GoalQuestionrequest> goalQuestionsrequests,GoalEntity goalEntity) {
List<GoalQuestionEntity> goalQuestionEntitIEs = goalQuestionsrequests
.stream()
.map(goalQuestionrequest -> {
GoalQuestionEntity goalQuestionEntity = new GoalQuestionEntity();
goalQuestionEntity.setID(goalQuestionrequest.getID());
goalQuestionEntity.settitle(goalQuestionrequest.gettitle());
goalQuestionEntity.setDescription(goalQuestionrequest.getDescription());
goalQuestionEntity.setGoal(goalEntity);
if (CollectionUtils.isnotEmpty(goalQuestionrequest.getGoalQuestionAnswers())) {
if (goalQuestionEntity.getGoalQuestionAnswers() == null) {
goalQuestionEntity.setGoalQuestionAnswers(new linkedList<>());
}
goalQuestionEntity.getGoalQuestionAnswers().clear();
goalQuestionEntity.getGoalQuestionAnswers().addAll(mapGoalQuestionAnswerrequestToEntity(goalQuestionrequest.getGoalQuestionAnswers(),goalQuestionEntity));
}
return goalQuestionEntity;
}).collect(Collectors.toList());
return goalQuestionEntitIEs;
}
private List<GoalQuestionANSWEREntity> mapGoalQuestionAnswerrequestToEntity(List<GoalQuestionAnswerrequest> goalQuestionAnswersrequests,GoalQuestionEntity goalQuestionEntity) {
List<GoalQuestionANSWEREntity> goalQuestionAnswersEntitIEs = goalQuestionAnswersrequests
.stream()
.map(goalQuestionAnswerrequest -> {
GoalQuestionANSWEREntity goalQuestionANSWEREntity = new GoalQuestionANSWEREntity();
goalQuestionANSWEREntity.setID(goalQuestionAnswerrequest.getID());
goalQuestionANSWEREntity.setText(goalQuestionAnswerrequest.getText());
goalQuestionANSWEREntity.setGoalQuestion(goalQuestionEntity);
return goalQuestionANSWEREntity;
})
.collect(Collectors.toList());
return goalQuestionAnswersEntitIEs;
}
问题是我收到错误 拥有级联的实体实例不再引用带有 cascade="all-delete-orphan" 的集合...
很抱歉给您发了这么长的消息, 感谢您的帮助
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
以上是大佬教程为你收集整理的Jpa 无法将 json 映射到每次都重新创建的实体全部内容,希望文章能够帮你解决Jpa 无法将 json 映射到每次都重新创建的实体所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。