Skip to content
20 changes: 15 additions & 5 deletions spring-data-jpa-2/initial/src/main/java/cholog/Author.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package cholog;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.*;

import java.util.HashSet;
import java.util.Set;

@Entity
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToOne
Person person;

@OneToMany(mappedBy = "author")
Set<BookAuthor> books = new HashSet<>();

public Author(Person person) {
this.person = person;
}

public Author() {
Expand All @@ -22,6 +28,10 @@ public Long getId() {
}

public Person getPerson() {
return null;
return person;
}

public Set<BookAuthor> getBooks() {
return books;
}
}
19 changes: 12 additions & 7 deletions spring-data-jpa-2/initial/src/main/java/cholog/Book.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package cholog;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.*;

import java.util.HashSet;
import java.util.Set;

@Entity
Expand All @@ -13,12 +11,19 @@ public class Book {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@ManyToOne
Publisher publisher;

@OneToMany(mappedBy = "book")
Set<BookAuthor> authors = new HashSet<>();

public Book() {

}

public Book(String name, Publisher publisher) {
this.name = name;
this.publisher = publisher;
}

public Long getId() {
Expand All @@ -30,10 +35,10 @@ public String getName() {
}

public Publisher getPublisher() {
return null;
return publisher;
}

public Set<Author> getAuthors() {
return null;
public Set<BookAuthor> getAuthors() {
return authors;
}
}
19 changes: 19 additions & 0 deletions spring-data-jpa-2/initial/src/main/java/cholog/BookAuthor.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
package cholog;

import jakarta.persistence.*;

@Entity
public class BookAuthor {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne
Book book;

@ManyToOne
Author author;

public BookAuthor(Book book, Author author) {
this.book = book;
this.author = author;
}

public BookAuthor() {

}
}
10 changes: 4 additions & 6 deletions spring-data-jpa-2/initial/src/main/java/cholog/Person.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package cholog;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.*;

@Entity
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;

@OneToOne(mappedBy = "person", fetch = FetchType.LAZY)
Author author;
public Person() {

}
Expand All @@ -29,6 +27,6 @@ public String getName() {
}

public Author getAuthor() {
return null;
return author;
}
}
12 changes: 6 additions & 6 deletions spring-data-jpa-2/initial/src/main/java/cholog/Publisher.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package cholog;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.*;

import java.util.HashSet;
import java.util.Set;

@Entity
Expand All @@ -13,6 +11,8 @@ public class Publisher {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@OneToMany(mappedBy = "publisher", fetch = FetchType.EAGER)
Set<Book> books = new HashSet<>();

public Publisher(String name) {
this.name = name;
Expand All @@ -30,10 +30,10 @@ public String getName() {
}

public void addBook(Book book) {

books.add(book);
}

public Set<Book> getBooks() {
return null;
return books;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ void biDirection() {
entityManager.persist(book);
publisher.addBook(book);

entityManager.flush();
entityManager.clear();
entityManager.flush(); // 영속성 컨텍스트에 있는 publisher와 book 엔티티의 상태(추가 및 연관관계 설정)가 데이터베이스에 반영됩니다. 즉, Publisher와 Book 객체가 실제 데이터베이스에 저장
entityManager.clear(); // 영속성 컨텍스트를 초기화하여 현재 관리되고 있는 모든 엔티티를 비영속 상태로 만듭니다. 이후 Publisher 객체를 조회할 때, 영속성 컨텍스트가 아닌 데이터베이스에서 해당 엔티티를 다시 가져오게 됩니다

Publisher persistPublisher = entityManager.find(Publisher.class, publisher.getId());
assertThat(persistPublisher).isNotNull();
Expand Down