Java 尝试使用外键通过邮递员发布json数据
我有两个实体,myAppUser和缺席,我试图创建一个与他的“所有者”相关的缺席,一个用户。缺勤情况张贴得很好,但缺勤与用户之间的关系从未发生过Java 尝试使用外键通过邮递员发布json数据,java,json,spring,jpa,postman,Java,Json,Spring,Jpa,Postman,我有两个实体,myAppUser和缺席,我试图创建一个与他的“所有者”相关的缺席,一个用户。缺勤情况张贴得很好,但缺勤与用户之间的关系从未发生过 @Entity public class Absence { @GeneratedValue(strategy=GenerationType.IDENTITY) @Id @JsonView(View.Summary.class) private int id; @Nullable @JsonVie
@Entity
public class Absence {
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Id
@JsonView(View.Summary.class)
private int id;
@Nullable
@JsonView(View.SummaryWithOthers.class)
private Date dateMissing;
@Nullable
@JsonProperty(access = Access.READ_WRITE)
@JsonView(View.SummaryWithOthers.class)
private String comment;
@ManyToOne (fetch = FetchType.EAGER)
@JsonView(View.Summary.class)
@JoinColumn (name="student_id", nullable = false)
private MyAppUser userStudent;
服务
public Absence createAbsence(Absence absence, MyAppUser userStudent) {
String studentId = userStudent.getId(); //absence.getUserStudent().getId();
Absence absenceCreated = new Absence();
userStudent = myAppUserRepository.findUserById(studentId);
absenceCreated.setComment(absence.getComment());
absenceCreated.setDateMissing(absence.getDateMissing());
absenceCreated.setUserStudent(userStudent);
myAbsenceRepository.save(absenceCreated);
return absenceCreated;
}
邮递员中的JSON
{
"userStudent":{
"student_id":"58a55a41-04cc-4007-a322-e7bb969e626b"
},
"dateMissing": "1574541388697",
"comment": "prueba"
}
它返回了这个:
{
"id": 485,
"dateMissing": 1574541388697,
"comment": "medic issue",
"userStudent": null
}
我不知道我是否输入了错误的JSON,或者我的服务是否出了问题,但我在春天真的很无聊,我不明白。。。只需要访问用户存储库并通过其字符串ID获取一个,我怎么能
谢谢你!!^^^ 是否应该是
返回缺席服务.create缺席(缺席,缺席.getUserStudent())
?您的控制器正在从主体中获取Absense
,但没有UserStudent对象的上下文(因此为空)。想法不错,但不起作用…:(也许我在json中键入了错误的内容?我很迷茫@kendavidsonI也尝试过使用缺席服务.create缺席(缺席,缺席.getUserStudent().getId())为了捕获ID,我正在通过json传递,但也不起作用……无论如何,谢谢@kendavidson!当您调试服务时,值是否正确?我对spring的调试非常糟糕,但我看到学生ID没有被捕获,并且直接设置为null@kendavidson它不应该是return absences service.createdemission吗(缺席,absense.getUserStudent())
?您的控制器正在从主体获取缺席
,但没有UserStudent对象的上下文(因此为空)。想法不错,但它不起作用…:(可能我在json中键入了错误的内容?我很迷路@kendavidsonI也尝试了缺席服务。CreateAbence(缺席,缺席。getUserStudent().getId())来捕获我正在通过json传递的ID,但也不起作用…无论如何,感谢@kendavidson!当您调试服务时,值是否正确?我用spring调试时非常糟糕,但我看到学生ID没有被捕获,并且它直接设置为null@kendavidson
public Absence createAbsence(Absence absence, MyAppUser userStudent) {
String studentId = userStudent.getId(); //absence.getUserStudent().getId();
Absence absenceCreated = new Absence();
userStudent = myAppUserRepository.findUserById(studentId);
absenceCreated.setComment(absence.getComment());
absenceCreated.setDateMissing(absence.getDateMissing());
absenceCreated.setUserStudent(userStudent);
myAbsenceRepository.save(absenceCreated);
return absenceCreated;
}
{
"userStudent":{
"student_id":"58a55a41-04cc-4007-a322-e7bb969e626b"
},
"dateMissing": "1574541388697",
"comment": "prueba"
}
{
"id": 485,
"dateMissing": 1574541388697,
"comment": "medic issue",
"userStudent": null
}