11package com .bookstore .entity ;
22
33import java .io .Serializable ;
4- import java .util .ArrayList ;
5- import java .util .Iterator ;
6- import java .util .List ;
7- import javax .persistence .CascadeType ;
84import javax .persistence .Entity ;
95import javax .persistence .GeneratedValue ;
106import javax .persistence .GenerationType ;
117import javax .persistence .Id ;
12- import javax .persistence .OneToMany ;
138
149@ Entity
1510public 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