Skip to content

Commit 655db53

Browse files
committed
Child-side parent association via proxy
1 parent 1e0fe50 commit 655db53

File tree

1 file changed

+3
-41
lines changed
  • HibernateSpringBootPopulatingChildViaProxy/src/main/java/com/bookstore/entity

1 file changed

+3
-41
lines changed
Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
package com.bookstore.entity;
22

33
import java.io.Serializable;
4-
import java.util.ArrayList;
5-
import java.util.Iterator;
6-
import java.util.List;
7-
import javax.persistence.CascadeType;
84
import javax.persistence.Entity;
95
import javax.persistence.GeneratedValue;
106
import javax.persistence.GenerationType;
117
import javax.persistence.Id;
12-
import javax.persistence.OneToMany;
138

149
@Entity
1510
public class Author implements Serializable {
@@ -24,31 +19,6 @@ public class Author implements Serializable {
2419
private String genre;
2520
private int age;
2621

27-
@OneToMany(cascade = CascadeType.ALL,
28-
mappedBy = "author", orphanRemoval = true)
29-
private List<Book> books = new ArrayList<>();
30-
31-
public void addBook(Book book) {
32-
this.books.add(book);
33-
book.setAuthor(this);
34-
}
35-
36-
public void removeBook(Book book) {
37-
book.setAuthor(null);
38-
this.books.remove(book);
39-
}
40-
41-
public void removeBooks() {
42-
Iterator<Book> iterator = this.books.iterator();
43-
44-
while (iterator.hasNext()) {
45-
Book book = iterator.next();
46-
47-
book.setAuthor(null);
48-
iterator.remove();
49-
}
50-
}
51-
5222
public Long getId() {
5323
return id;
5424
}
@@ -71,7 +41,7 @@ public String getGenre() {
7141

7242
public void setGenre(String genre) {
7343
this.genre = genre;
74-
}
44+
}
7545

7646
public int getAge() {
7747
return age;
@@ -81,18 +51,10 @@ public void setAge(int age) {
8151
this.age = age;
8252
}
8353

84-
public List<Book> getBooks() {
85-
return books;
86-
}
87-
88-
public void setBooks(List<Book> books) {
89-
this.books = books;
90-
}
91-
9254
@Override
9355
public String toString() {
94-
return "Author{" + "id=" + id + ", name=" + name
56+
return "Author{" + "id=" + id + ", name=" + name
9557
+ ", genre=" + genre + ", age=" + age + '}';
9658
}
97-
59+
9860
}

0 commit comments

Comments
 (0)