Dev/JPA

JPA M:N ์—ฐ๊ด€๊ด€๊ณ„

OK-๊ฐ€์ž 2022. 3. 2. 17:26
๐Ÿ˜ˆ ์ฐธ๊ณ ๋กœ 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 ์—ฐ๊ด€๊ด€๊ณ„ ๋งŽ์ด ๋ณต์žกํ• ์ˆ˜ ์žˆ์ง€๋งŒ, ์ž˜ ํ™œ์šฉํ•˜๋ฉด ์ •๋ง ์ข‹์„๊บผ๊ฐ™๋‹ค. ๋นจ๋ฆฌ ์‹ค์ „์— ์จ๋ณด๊ณ  ์‹ถ๋‹ค.