Skip to content

Commit efec0cf

Browse files
adding relationships example
1 parent 15c2a5a commit efec0cf

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

src/main/java/guru/springframework/spring6webapp/domain/Author.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package guru.springframework.spring6webapp.domain;
22

3-
import jakarta.persistence.Entity;
4-
import jakarta.persistence.GeneratedValue;
5-
import jakarta.persistence.GenerationType;
6-
import jakarta.persistence.Id;
3+
import jakarta.persistence.*;
4+
5+
import java.util.Set;
76

87
/**
98
* Created by jt, Spring Framework Guru.
@@ -17,6 +16,17 @@ public class Author {
1716
private String firstName;
1817
private String lastName;
1918

19+
@ManyToMany(mappedBy = "authors")
20+
private Set<Book> books;
21+
22+
public Set<Book> getBooks() {
23+
return books;
24+
}
25+
26+
public void setBooks(Set<Book> books) {
27+
this.books = books;
28+
}
29+
2030
public Long getId() {
2131
return id;
2232
}

src/main/java/guru/springframework/spring6webapp/domain/Book.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package guru.springframework.spring6webapp.domain;
22

3-
import jakarta.persistence.Entity;
4-
import jakarta.persistence.GeneratedValue;
5-
import jakarta.persistence.GenerationType;
6-
import jakarta.persistence.Id;
3+
import jakarta.persistence.*;
4+
5+
import java.util.Set;
76

87
/**
98
* Created by jt, Spring Framework Guru.
@@ -17,6 +16,19 @@ public class Book {
1716
private String title;
1817
private String isbn;
1918

19+
@ManyToMany
20+
@JoinTable(name = "author_book", joinColumns = @JoinColumn(name = "book_id"),
21+
inverseJoinColumns = @JoinColumn(name = "author_id"))
22+
private Set<Author> authors;
23+
24+
public Set<Author> getAuthors() {
25+
return authors;
26+
}
27+
28+
public void setAuthors(Set<Author> authors) {
29+
this.authors = authors;
30+
}
31+
2032
public Long getId() {
2133
return id;
2234
}

0 commit comments

Comments
 (0)