11package org .baeldung .persistence .model ;
22
33import java .io .Serializable ;
4- import java .util .List ;
5-
6- import javax .persistence .Column ;
7- import javax .persistence .Entity ;
8- import javax .persistence .GeneratedValue ;
9- import javax .persistence .GenerationType ;
10- import javax .persistence .Id ;
4+ import java .util .Set ;
5+ import com .google .common .collect .Sets ;
6+ import javax .persistence .*;
7+ import org .hibernate .annotations .OrderBy ;
118
129@ Entity
10+ @ NamedQuery (name = "Bar.findAll" , query = "SELECT b FROM Bar b" )
1311public class Bar implements Serializable {
1412
15- @ Id
16- @ GeneratedValue (strategy = GenerationType .AUTO )
17- private long id ;
18-
19- @ Column (nullable = false )
20- private String name ;
21-
22- private List <Foo > foos ;
23-
2413 public Bar () {
2514 super ();
2615 }
2716
2817 public Bar (final String name ) {
2918 super ();
30-
19+
3120 this .name = name ;
3221 }
3322
34- // API
23+ private static final long serialVersionUID = 1L ;
24+ @ Id
25+ @ GeneratedValue (strategy = GenerationType .AUTO )
26+ private int id ;
27+
28+ private String name ;
29+
30+ @ OneToMany (mappedBy = "bar" , cascade = CascadeType .ALL , fetch = FetchType .EAGER )
31+ @ OrderBy (clause = "NAME DESC" )
32+ Set <Foo > fooSet = Sets .newHashSet ();
33+
34+ //API
35+
36+ public Set <Foo > getFooSet () {
37+ return fooSet ;
38+ }
39+
40+ public void setFooList (Set <Foo > fooSet ) {
41+ this .fooSet = fooSet ;
42+ }
3543
36- public long getId () {
37- return id ;
44+ public int getId () {
45+ return this . id ;
3846 }
3947
40- public void setId (final long id ) {
48+ public void setId (int id ) {
4149 this .id = id ;
4250 }
4351
4452 public String getName () {
45- return name ;
53+ return this . name ;
4654 }
4755
48- public void setName (final String name ) {
56+ public void setName (String name ) {
4957 this .name = name ;
5058 }
51-
52- public List <Foo > getFooList () {
53- return foos ;
54- }
55-
5659 //
57-
60+
5861 @ Override
5962 public int hashCode () {
6063 final int prime = 31 ;
6164 int result = 1 ;
6265 result = prime * result + ((name == null ) ? 0 : name .hashCode ());
6366 return result ;
6467 }
65-
68+
6669 @ Override
6770 public boolean equals (final Object obj ) {
6871 if (this == obj )
@@ -79,12 +82,13 @@ public boolean equals(final Object obj) {
7982 return false ;
8083 return true ;
8184 }
82-
85+
8386 @ Override
8487 public String toString () {
8588 final StringBuilder builder = new StringBuilder ();
86- builder .append ("Foo [name=" ).append (name ).append ("]" );
89+ builder .append ("Bar [name=" ).append (name ).append ("]" );
8790 return builder .toString ();
8891 }
8992
93+
9094}
0 commit comments