I am very new to Hibernate and I am trying to solve an issue similar to this question, specifically the answer.
I have something very similar set up (in my case it's Author, Book, and the mapping table AuthorBook). I am using this in Spring Data JPA, so I have the following components:
- Repositories: AuthorRepository,BookRepository,AuthorBookRepository
- Services: AuthorService,BookService,AuthorBookRepository
- Controllers: AuthorController,BookController,AuthorBookController
My entities are:
@Entity
public class Author {
    @Id
    @GeneratedValue
    private Long id;
    private String name;
    @OneToMany(mappedBy = "author")
    private Set<AuthorBook> authorBooks;
    // getters and setters
}
@Entity
public class Book {
    @Id
    @GeneratedValue
    private Long id;
    private String name;
    @OneToMany(mappedBy = "book")
    private Set<AuthorBook> authorBooks;
    // getters and setters
}
@Entity
public class AuthorBook {
    @Id
    @GeneratedValue
    private Integer id;
    @ManyToOne
    @JoinColumn(name = "user_id")
    private Author author;
    @ManyToOne
    @JoinColumn(name = "book_id")
    private Book book;
    @Column(name = "isMainAuthor")
    private boolean isMainAuthor;
  
    // getters and setter
}
My understanding is that I should make the following POST requests:
Create an author:
{
    "name": "Test Author"
}
Create a book:
{
    "name": "Test Book"
}
Create the mapping:
{
    "author": {
        "id": 1
    },
    "book": {
        "id": 2
    },
    "isMainAuthor": true
}
First of all: is this the correct way to use this? If no, how should I use it instead? If yes, how is serialisation supposed to work? Because if I do it like this, and then fetch the list of books, the response will be infinitely long like so:
[
    {
        "id": 2,
        "name": "Test Book",
        "authorBooks": [
            {
                "id": 3,
                "author": {
                    "id": 1,
                    "name": "Test Author",
                    "authorBooks": [
                        {
                            "id": 3,
                            "author": {
                                "id": 1,
                                "name": "Test Author",
                                "authorBooks": [
                                    {
                                        "id": 3,
                                        "author": {
                                            "id": 1,
                                            "name": "Test Author",
                                            "authorBooks": [
    ...
I know that I could use @JsonIgnore on the Author and Book getters in AuthorBook, and @JsonProperty on the setters so that deserialisation still works. However, as someone who is unexperienced with Hibernate, this seems like a hack to me. What is the cleanest and best way to solve this?
Also, is there a way to update the mapping via the author and book endpoints directly? I might already have an author in the database and want to just add a new book he wrote by adding a new Book entity and providing the relation as part of it, but I seem not to be able to do that.
 
     
    