当向复合 table (Join table) 中插入值时,它使其他两个 table 值为 null Spring JPA

When insert values to composite table (Join table), it makes other two table values as null Spring JPA

书籍Class

@Component
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Entity
@Table(name = "books")
@JsonIdentityInfo(
        generator = ObjectIdGenerators.PropertyGenerator.class,
        property = "bookId", scope = Books.class)
public class Books {


    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "bookId")
    private Long bookId;
    @Column(unique = true)
    private String book_reference;
    private String isbn;
    private String title;
    private String author;
    private String publication;
    private String edition;
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
    private Date published_year;
    private String category;
    private int number_of_copies;

    @OneToMany(mappedBy = "books", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<IssuedBooks> issuedBooks;


    public Books(String book_reference, String  isbn, String title, String author, String publication, String edition, Date published_year, String category, int number_of_copies) {
        this.book_reference = book_reference;
        this.isbn = isbn;
        this.title = title;
        this.author = author;
        this.publication = publication;
        this.edition = edition;
        this.published_year = published_year;
        this.category = category;
        this.number_of_copies = number_of_copies;
    }

}

学生Class

@Component
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Entity
@Table(name = "students")
@JsonIdentityInfo(
        generator = ObjectIdGenerators.PropertyGenerator.class,
        property = "studentId", scope = Students.class)
public class Students {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "studentId")
    private Long studentId;
    private String first_name;
    private String last_name;
    @Column(unique = true)
    private String email;
    private String address;
    private String telephone;
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
    private Date registered_date;
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
    private Date terminated_date;

    @OneToMany(mappedBy = "students", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<IssuedBooks> issuedBooks;


    public Students(String first_name, String last_name, String email, String address, String telephone, Date registered_date, Date terminated_date) {
        this.first_name = first_name;
        this.last_name = last_name;
        this.email = email;
        this.address = address;
        this.telephone = telephone;
        this.registered_date = registered_date;
        this.terminated_date = terminated_date;
    }

}

加入TableClasses 我加入了两个实体 Students 和 Books

IssuedBooksId Class

@Embeddable
public class IssuedBooksId implements Serializable {
    @Column(name = "bookId")
    private Long bookId;
    @Column(name = "studentId")
    private Long studentId;


    public IssuedBooksId() {
    }

    public IssuedBooksId(Long bookId, Long studentId) {
        this.bookId = bookId;
        this.studentId = studentId;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        IssuedBooksId that = (IssuedBooksId) o;
        return bookId.equals(that.bookId) &&
                studentId.equals(that.studentId);
    }

    @Override
    public int hashCode() {
        return Objects.hash(bookId, studentId);
    }
}

发行图书Class/

@Data
@Entity
@Table(name = "issuedBooks")
@JsonIdentityInfo(scope = IssuedBooks.class,
        generator = ObjectIdGenerators.PropertyGenerator.class,
        property = "issuedBooksId")
public class IssuedBooks {


    @EmbeddedId

    private IssuedBooksId issuedBooksId = new IssuedBooksId();


    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @MapsId("bookId")
    @JoinColumn(name = "bookId")
    private Books books;

    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @MapsId("studentId")
    @JoinColumn(name = "studentId")
    private Students students;

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
    private Date issueDate;
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
    private Date returnDate;


    public IssuedBooks() {
    }

    public IssuedBooks(Books books, Students students) {
        this.books = books;
        this.students = students;
        this.issuedBooksId = new IssuedBooksId(books.getBookId(), students.getStudentId());
    }

    public IssuedBooks(Books books, Students students, Date issueDate, Date returnDate) {
        this.books = books;
        this.students = students;
        this.issuedBooksId = new IssuedBooksId(books.getBookId(), students.getStudentId());
        this.issueDate = issueDate;
        this.returnDate = returnDate;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        IssuedBooks that = (IssuedBooks) o;
        return books.equals(that.books) &&
                students.equals(that.students);
    }

    @Override
    public int hashCode() {
        return Objects.hash(books, students);
    }
}

我试过 @JsonIdentityInfo 如下

ObjectIdGenerators.IntSequenceGenerator.class ObjectIdGenerators.PropertyGenerator ObjectIdGenerators.UUIDGenerator

向 IssueBooks 插入值后 table 其他 table 像这样

书籍Table

学生Table

IssueBooks Table

我解决了这个问题。

解决办法是;应该使子 class cascadeType 为 Persist

@EmbeddedId
IssueBooksId issueBooksId = new IssueBooksId();
    
@ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
@MapsId("bookId")
@JoinColumn(name = "bookId")
private Books books;
    
@ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
@MapsId("studentId")
@JoinColumn(name = "studentId")
private Students students;

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
private Date issueDate;

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
private Date returnDate;