У меня есть две сущности, myAppUser и Absences, и я пытаюсь создать Absence, связанную с его «владельцем», пользователем. Отсутствия опубликованы хорошо, но связь между отсутствием и пользователем никогда не происходит.
@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;
@Entity
@Table(name="users")
public class MyAppUser {
//@GeneratedValue(strategy=GenerationType.IDENTITY)
@Id
@JsonView(View.Summary.class)
private String id;
@JsonView(View.Summary.class)
private String firstName;
@JsonView(View.Summary.class)
private String lastName;
@JsonView(View.SummaryWithOthers.class)
private String email;
@JsonView(View.SummaryWithOthers.class)
private char gender;
@JsonView(View.SummaryWithOthers.class)
private int age;
@JsonView(View.SummaryWithOthers.class)
private String portrait;
@JsonView(View.Summary.class)
@ManyToOne
private Seat seat;
@JsonView(View.SummaryWithOthers.class)
@ManyToOne
private Absence userAbsence;
@ManyToOne (fetch = FetchType.EAGER)
@JoinColumn (name="rol_id")
private Role role;
private String password;
private boolean enabled;
private Date lastLogin;
@OneToMany (targetEntity = Absence.class, cascade = CascadeType.ALL)
private List <Absence> absences = new ArrayList <Absence>();
@OneToMany (targetEntity = Course.class, cascade = CascadeType.ALL)
@JsonView(View.Summary.class)
public List <Course> courses = new ArrayList <Course>();
@OneToMany (targetEntity = UserExercice.class, cascade = CascadeType.ALL)
private List <UserExercice> userExercices = new ArrayList <UserExercice>();
@OneToMany (targetEntity = Emails.class, cascade = CascadeType.ALL)
private List <Emails> emails = new ArrayList <Emails>();
@OneToMany(mappedBy="myAppUser")
@JsonIgnore
Set<UserIteration> userIterations;
MyAppUser Repository
public interface MyAppUserRepository extends JpaRepository<MyAppUser, String>{
public ArrayList<MyAppUser> findByFirstName(String firstName);
public ArrayList<MyAppUser> findByLastName(String lastName);
public List<MyAppUser> findByGender(char gender);
public ArrayList<MyAppUser> findByRoleId(int roleId);
Optional <MyAppUser> findOneById(String id );
MyAppUser findUserById(String id );
}
Контроллер
//Create absence by student id
@PostMapping("/api/students/absences")
public Absence createAbsence(@RequestBody Absence absence, MyAppUser userStudent) {
return absencesService.createAbsence(absence, 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 в POSTMAN
{
"userStudent":{
"student_id":"58a55a41-04cc-4007-a322-e7bb969e626b"
},
"dateMissing": "1574541388697",
"comment": "prueba"
}
И он возвращает это:
{
"id": 485,
"dateMissing": 1574541388697,
"comment": "medic issue",
"userStudent": null
}
Я не знаю, набираю ли я JSON неправильно, или если мой сервис работает неправильно, но я по-настоящему noob в Spring и не понимаю ... Просто нужно получить доступ к репозиторию пользователя и получить его по его String ID. Как я могу?
Благодарю вас !! ^^