Issue
It's so working well when I was trying to search "nice_library" from "book". I guess because the nice_library FK is saved on the book.
book = bookRepository.findById(book.getId()).orElseThrow(() -> new RuntimeException());
Long id = book.getNiceLibrary().getId();
assertEquals(id, niceLibrary.getId());
but I can't get anything when I tried to search "book" from "nice_library". I guess because nice_library doesn't have FK of book
niceLibrary = niceLibraryRepository.findById(niceLibrary.getId()).orElseThrow(() -> new RuntimeException());
List<Book> books = niceLibrary.getBooks(); // WHAT THE
I was expecting the "Book.id" to be saved on the "nice_library" table for search to "book" from "nice_library".
Book.java
package com.example.jpa;
import lombok.Getter;
import lombok.Setter;
import javax.persistence.*;
@Getter
@Entity
public class Book {
@Id
@GeneratedValue
@Column
private Long id;
@Setter
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "LIBRARY_ID") // Foreign Key
private NiceLibrary niceLibrary;
}
NiceLibrary.java
package com.example.jpa;
import lombok.Getter;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Getter
@Entity
public class NiceLibrary {
@Id
@GeneratedValue
@Column
private Long id;
@OneToMany(mappedBy = "niceLibrary")
private List<Book> books = new ArrayList<>();
}
book table
id|library_id|
--+----------+
77| 78|
nice_library
id|
--+
78|
Test Code
@SpringBootTest
class BookServiceTest {
@Autowired
private BookRepository bookRepository;
@Autowired
private NiceLibraryRepository niceLibraryRepository;
@Test
public void book() {
Book book = new Book();
NiceLibrary niceLibrary = new NiceLibrary();
niceLibrary.getBooks().add(book);
book.setNiceLibrary(niceLibrary);
bookRepository.save(book);
book = bookRepository.findById(book.getId()).orElseThrow(() -> new RuntimeException());
Long id = book.getNiceLibrary().getId();
assertEquals(id, niceLibrary.getId());
niceLibrary = niceLibraryRepository.findById(niceLibrary.getId()).orElseThrow(() -> new RuntimeException());
List<Book> books = niceLibrary.getBooks(); // ???
}
How can I search book from nice_library?
Solution
From what I understand you want to be able to get a specific book from the library.
The one-to-many relationship is setup by having a reference to the library in the book table.
There is no reason to store the book's id in the library table.
This is all you need to setup a one-to-many relationship and you have this correct.
+-----------------+
| Book |
+-----------------+
| id |
| LIBRARY_ID (fk) |
+-----------------+
+------------+
| Library |
+------------+
| id |
+------------+
The way you are describing the problem is that you are expecting a library to only have one book, which is about as big as my library.
In a one-to-many relationship the one side (library in this case) will always have a collection of entities and the other side (book in this case) will only have a reference to a single entity.
Answered By - TRWP
Answer Checked By - Terry (JavaFixing Volunteer)