我创建了两个表格-学生和主题。 然后,我创建了第三个表student_subject,这是一个将学生与科目联系起来的联合表。它有5个字段:行ID,学生ID,学科ID和created_at和Updated_at字段(来自AuditModel类):
package com.rest.web.postgresapi.data.models;
import com.fasterxml.jackson.annotation.JsonIgnore;
import javax.persistence.*;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
@Entity
@Table(uniqueConstraints = {
@UniqueConstraint(columnNames = {"student_id", "subject_id"})
})
public class StudentSubject extends AuditModel {
@Id
@GeneratedValue(generator = "enrollment_generator")
@SequenceGenerator(
name = "enrollment_generator",
sequenceName = "enrollment_sequence",
initialValue = 4420
)
private Long id;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "student_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
@JsonIgnore
private Student student_id; // If I put private Long student_id, it fails
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "subject_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
@JsonIgnore
private Subject subject_id; // If I put private Long subject_id, it fails
// Constructors
protected StudentSubject() {}
public StudentSubject(Student student_id, Subject subject_id) {
this.student_id = student_id;
this.subject_id = subject_id;
}
// Getters
public Long getId() {
return id;
}
public Student getStudent_id() {
return student_id;
}
public Subject getSubject_id() {
return subject_id;
}
// Setters
public void setId(Long id) {
this.id = id;
}
public void setStudent_id(Student student) {
this.student_id = student;
}
public void setSubject_id(Subject subject) {
this.subject_id = subject;
}
}
应用程序完美地在数据库中创建了表格,我可以在student和subject表格中进行获取和发布。没问题。疼痛来自关节台控制器。
这是student_subject联合表表的控制器
package com.rest.web.postgresapi.controllers;
import com.rest.web.postgresapi.data.models.StudentSubject;
import com.rest.web.postgresapi.repository.StudentSubjectRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
import java.util.List;
@RestController
public class StudentSubjectController {
@Autowired
private StudentSubjectRepository studentSubjectRepository;
@GetMapping("/enrollments")
public List<StudentSubject> getAllStudentsSubjects(){
return studentSubjectRepository.findAll();
}
@PostMapping("/enrollments/student/subject")
public StudentSubject createStudentSubject(@Valid @RequestBody StudentSubject studentSubject) {
return studentSubjectRepository.save(studentSubject);
}
}
有两个问题:
1 .-当我从student_subject表中进行获取时,它仅检索行的ID以及created_at和Updated_at字段。没有student_id或subject_id。
2 .--当我(从邮递员)张贴帖子以插入一行时,出现以下错误:
Detail: Failing row contains (4671, 2018-11-20 11:04:34.176, 2018-11-20 11:04:34.176, null, null).
我同时提供了Student_id和subject_id,如您在邮递员的此屏幕截图中所见,但错误明确指出两个字段均为空:
看来我对表格的定义有误。我的代码中缺少什么?
答案 0 :(得分:0)
Spring MVC使用Jackson来将Java对象与JSON序列化/反序列化。
如果使用 @JSONIgnore 注释属性,那么Jackson将忽略它。
这就是为什么在GET方法的JSON响应中看不到 student_id 字段或 subject_id 字段的原因。因为当从对象转换为json时,杰克逊会忽略它们。
这就是您的POST失败的原因。因为Jackson忽略了接收到的属性。 Jackson正在创建一个空实体,而JPA正在保存没有 student_id 和 subject_id 的实体。
答案 1 :(得分:0)