Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.fasterxml.jackson.annotation.JsonManagedReference;
- import jakarta.persistence.*;
- import java.util.List;
- @Entity
- @Table(name = "authors")
- public class Author {
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- // @Column(name = "id")
- private long id;
- @Column
- private String name;
- @ManyToMany
- @JsonManagedReference // This is to avoid infinite loop. This is forward part of reference.
- @JoinTable(
- name = "AUTHOR_BOOK_MAPPING",
- joinColumns = @JoinColumn(name = "author_id"),
- inverseJoinColumns = @JoinColumn(name = "book_id"))
- private List<Book> books; // no need to specify implementation, Hibernate will use its own
- public Author() {
- }
- public Author(String name) {
- this.name = name;
- }
- public Long getId() {
- return id;
- }
- public void setId(long id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public List<Book> getBooks() {
- return books;
- }
- public void setBooks(List<Book> books) {
- this.books = books;
- }
- public void addBook(Book book) {
- books.add(book);
- book.getAuthors().add(this);
- }
- public void removeBook(Book book) {
- books.remove(book);
- book.getAuthors().remove(this);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment