Books 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;
}
}
Student 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;
}
}
Join Table Classes I have join two entity Students and 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);
}
}
IssuedBooks 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);
}
}
I tried @JsonIdentityInfo as belows
ObjectIdGenerators.IntSequenceGenerator.class ObjectIdGenerators.PropertyGenerator ObjectIdGenerators.UUIDGenerator
After Inserting values to IssueBooks table other tables like this
Books Table
Student Table
IssueBooks Table
CodePudding user response:
I resolved this problem.
The solution is; should make child class cascadeType as 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;