Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion spring-data-jpa-2/initial/src/main/java/cholog/Author.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,41 @@
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OneToOne;

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

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

@OneToOne
private Person person;

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

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

public Author() {

}

public Long getId() {
return id;
}

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

public Set<BookAuthor> getBooks() {
return books;
}
}
21 changes: 16 additions & 5 deletions spring-data-jpa-2/initial/src/main/java/cholog/Book.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;

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

@Entity
Expand All @@ -14,11 +17,19 @@ public class Book {
private Long id;
private String name;

public Book() {
@ManyToOne
private Publisher publisher;

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

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

public Book() {

}

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

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

public Set<Author> getAuthors() {
return null;
public Set<BookAuthor> getAuthors() {
return authors;
}
}
33 changes: 33 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,40 @@
package cholog;

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

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

@ManyToOne
private Book book;

@ManyToOne
private Author author;

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

public BookAuthor() {
}

public Long getId() {
return id;
}

public Book getBook() {
return book;
}

public Author getAuthor() {
return author;
}
}
12 changes: 8 additions & 4 deletions spring-data-jpa-2/initial/src/main/java/cholog/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToOne;

@Entity
public class Person {
Expand All @@ -12,14 +13,17 @@ public class Person {
private Long id;
private String name;

public Person() {

}
@OneToOne(mappedBy = "person")
private Author author;

public Person(String name) {
this.name = name;
}

public Person() {

}

public Long getId() {
return id;
}
Expand All @@ -29,6 +33,6 @@ public String getName() {
}

public Author getAuthor() {
return null;
return author;
}
}
13 changes: 9 additions & 4 deletions spring-data-jpa-2/initial/src/main/java/cholog/Publisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;

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

@Entity
Expand All @@ -14,6 +16,9 @@ public class Publisher {
private Long id;
private String name;

@OneToMany(mappedBy = "publisher")
private Set<Book> books = new HashSet<>();

public Publisher(String name) {
this.name = name;
}
Expand All @@ -29,11 +34,11 @@ public String getName() {
return name;
}

public void addBook(Book book) {

public Set<Book> getBooks() {
return books;
}

public Set<Book> getBooks() {
return null;
public void addBook(Book book) {
this.books.add(book);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
public class ManyToManyTest {
@Autowired
private BookRepository bookRepository;
@Autowired
private AuthorRepository authorRepository;
Comment on lines +17 to +18

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

추가한 이유가 있나요?


@PersistenceContext
private EntityManager entityManager;
Expand Down