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
16 changes: 11 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.Set;

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

@OneToOne
private Person person;

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

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

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

public Person getPerson() {
return null;
return person;
}
}
20 changes: 14 additions & 6 deletions spring-data-jpa-2/initial/src/main/java/cholog/Book.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package cholog;

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

import java.util.Set;
import java.util.stream.Collectors;

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

@ManyToOne
private Publisher publisher;

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

public Book() {

}

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

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

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

public Set<Author> getAuthors() {
return null;
return authors.stream()
.map(BookAuthor::getAuthor)
.collect(Collectors.toSet());
}
}
25 changes: 25 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,32 @@
package cholog;

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

@Entity
public class BookAuthor {

@Id @GeneratedValue
private Long id;

@ManyToOne
private Book book;

@ManyToOne
private Author author;

public BookAuthor() {

}

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

public Author getAuthor() {
return author;
}
}
10 changes: 5 additions & 5 deletions spring-data-jpa-2/initial/src/main/java/cholog/Person.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
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 {
Expand All @@ -12,6 +9,9 @@ public class Person {
private Long id;
private String name;

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

public Person() {

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

public Author getAuthor() {
return null;
return author;
}
}
15 changes: 9 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,9 @@
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.List;
import java.util.Set;

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

@OneToMany(mappedBy = "publisher", fetch = FetchType.EAGER)
private Set<Book> books;

public Publisher(String name) {
this.name = name;
this.books = new HashSet<>();
}

public Publisher() {
Expand All @@ -30,10 +33,10 @@ public String getName() {
}

public void addBook(Book book) {

books.add(book);
}

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