๐ ์ฐธ๊ณ ๋ก ManyToMany๋ ์ ์์ฐ๊ณ ์ค๊ฐ์ ๋งตํ ์ํฐํฐ(ํ
์ด๋ธ)์ ๋ง๋ค์ด์ ์ฌ์ฉํ๋ค. BookAndAuthor๋ผ๊ณ ๋ง๋ค๊บผ์ง๋ง ๋ค์ด๋ฐ์ ์๋ ์ด๋ ๊ฒ ํ๋ฉด ์๋๋ค.
Author Entity
@Entity
@NoArgsConstructor
@Data
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class Author extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String country;
// @ManyToMany
@OneToMany
@JoinColumn(name = "author_id")
@ToString.Exclude
private List<BookAndAuthor> bookAndAuthors = new ArrayList<>();
public void addBookAndAuthors(BookAndAuthor... bookAndAuthors) {
Collections.addAll(this.bookAndAuthors, bookAndAuthors);
}
}
Book Entity
...
...
@OneToMany
@JoinColumn(name = "book_id")
@ToString.Exclude
private List<BookAndAuthor> bookAndAuthors = new ArrayList<>();
public void addBookAndAuthors(BookAndAuthor... bookAndAuthors) {
Collections.addAll(this.bookAndAuthors, bookAndAuthors);
}
...
...
BookAndAuthor Entity
@Entity
@NoArgsConstructor
@Data
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class BookAndAuthor extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
private Book book;
@ManyToOne
private Author author;
}
TEST ํด๋ณด์
@Test
@Transactional
void manyToManyTest() {
Book book1 = givenBook("์ฑ
1");
Book book2 = givenBook("์ฑ
2");
Book book3 = givenBook("๊ฐ๋ฐ์ฑ
1");
Book book4 = givenBook("๊ฐ๋ฐ์ฑ
2");
Author author1 = givenAuthor("martin");
Author author2 = givenAuthor("steve");
BookAndAuthor bookAndAuthor1 = givenBookAndAuthor(book1, author1);
BookAndAuthor bookAndAuthor2 = givenBookAndAuthor(book2, author2);
// ๊ณต๋ ์ ์
BookAndAuthor bookAndAuthor3 = givenBookAndAuthor(book3, author1);
BookAndAuthor bookAndAuthor4 = givenBookAndAuthor(book3, author2);
// ๊ณต๋ ์ ์
BookAndAuthor bookAndAuthor5 = givenBookAndAuthor(book4, author1);
BookAndAuthor bookAndAuthor6 = givenBookAndAuthor(book4, author2);
// book1.addAuthor(author1);
// book2.addAuthor(author2);
// book3.addAuthor(author1, author2);
// book4.addAuthor(author1, author2);
//
// author1.addBook(book1, book3, book4);
// author2.addBook(book2, book3, book4);
book1.addBookAndAuthors(bookAndAuthor1);
book2.addBookAndAuthors(bookAndAuthor2);
book3.addBookAndAuthors(bookAndAuthor3, bookAndAuthor4);
book4.addBookAndAuthors(bookAndAuthor5, bookAndAuthor6);
author1.addBookAndAuthors(bookAndAuthor1, bookAndAuthor3, bookAndAuthor5);
author2.addBookAndAuthors(bookAndAuthor2, bookAndAuthor4, bookAndAuthor6);
bookRepository.saveAll(Lists.newArrayList(book1, book2, book3, book4));
authorRepository.saveAll(Lists.newArrayList(author1, author2));
// System.out.println("authors through book : " + bookRepository.findAll().get(2).getAuthors());
// System.out.println("books through author : " + authorRepository.findAll().get(0).getBooks());
bookRepository.findAll().get(2).getBookAndAuthors().forEach(o -> System.out.println(o.getAuthor()));
authorRepository.findAll().get(0).getBookAndAuthors().forEach(o -> System.out.println(o.getBook()));
}
์ฐ๊ด๊ด๊ณ๋ ์ฌ๊ธฐ์ ๋์ด๋ค. JPA ์ฐ๊ด๊ด๊ณ ๋ง์ด ๋ณต์กํ ์ ์์ง๋ง, ์ ํ์ฉํ๋ฉด ์ ๋ง ์ข์๊บผ๊ฐ๋ค. ๋นจ๋ฆฌ ์ค์ ์ ์จ๋ณด๊ณ ์ถ๋ค.