11package org .baeldung .persistence .model ;
22
33import java .io .Serializable ;
4- import java .util .List ;
54
65import javax .persistence .Column ;
76import javax .persistence .Entity ;
87import javax .persistence .GeneratedValue ;
98import javax .persistence .GenerationType ;
109import javax .persistence .Id ;
10+ import javax .persistence .CascadeType ;
1111
1212@ Entity
1313public class Bar implements Serializable {
14-
14+
1515 @ Id
1616 @ GeneratedValue (strategy = GenerationType .AUTO )
1717 private long id ;
18-
18+
1919 @ Column (nullable = false )
2020 private String name ;
21-
22- private List <Foo > foos ;
23-
21+
22+ @ OneToMany (mappedBy = "bar" , fetch = FetchType .EAGER , cascade = CascadeType .ALL )
23+ @ OrderBy ("name ASC" )
24+ List <Foo > fooList ;
25+
2426 public Bar () {
2527 super ();
2628 }
27-
29+
2830 public Bar (final String name ) {
2931 super ();
30-
32+
3133 this .name = name ;
3234 }
33-
35+
3436 // API
35-
37+
3638 public long getId () {
3739 return id ;
3840 }
39-
41+
4042 public void setId (final long id ) {
4143 this .id = id ;
4244 }
43-
45+
4446 public String getName () {
4547 return name ;
4648 }
47-
49+
4850 public void setName (final String name ) {
4951 this .name = name ;
5052 }
51-
53+
5254 public List <Foo > getFooList () {
53- return foos ;
55+ return fooList ;
5456 }
55-
57+
58+ public void setFooList (final List <Foo > fooList ) {
59+ this .fooList = fooList ;
60+ }
61+
5662 //
57-
63+
5864 @ Override
5965 public int hashCode () {
6066 final int prime = 31 ;
6167 int result = 1 ;
6268 result = prime * result + ((name == null ) ? 0 : name .hashCode ());
6369 return result ;
6470 }
65-
71+
6672 @ Override
6773 public boolean equals (final Object obj ) {
6874 if (this == obj )
@@ -79,12 +85,12 @@ public boolean equals(final Object obj) {
7985 return false ;
8086 return true ;
8187 }
82-
88+
8389 @ Override
8490 public String toString () {
8591 final StringBuilder builder = new StringBuilder ();
86- builder .append ("Foo [name=" ).append (name ).append ("]" );
92+ builder .append ("Bar [name=" ).append (name ).append ("]" );
8793 return builder .toString ();
8894 }
89-
95+
9096}
0 commit comments