有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaSpringJPA联合表不会返回所有字段

我创建了两个表格——学生和科目。 然后我创建了第三个表student_subject,这是一个连接学生和科目的联合表。它有5个文件:行id、学生id、科目id以及创建的_at和更新的_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联合表的控制器

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以及创建的_at和更新的_at字段。没有学生证,也没有学科证

response from get

2.-当我在post(从postman)中插入一行时,我得到了以下错误:

Detail: Failing row contains (4671, 2018-11-20 11:04:34.176, 2018-11-20 11:04:34.176, null, null).

我同时提供了学生id和科目id,正如您在postman的屏幕截图中所看到的,但错误明确表明这两个字段均为空:

postman post

我对桌子的定义似乎有点错误。我的代码中缺少了什么


共 (2) 个答案

  1. # 1 楼答案

    Spring MVC使用Jackson将Java对象序列化/反序列化到JSON或从JSON中序列化

    如果你用@JSONIgnore注释一个属性,那么Jackson会忽略它

    这就是为什么在GET方法的JSON响应中看不到student_id字段或subject_id字段的原因。因为Jackson在从object转换为json时忽略了它们

    这就是你的帖子失败的原因。因为Jackson忽略了接收到的属性。Jackson正在创建一个空实体,JPA正在保存该实体,但没有学生id主题id

  2. # 2 楼答案

    通过替换解决

    @JsonIgnore

    @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})

    如本answer所示