1 package ejava.jpa.examples.tuning.bo; 2 3 import java.text.DateFormat; 4 import java.text.SimpleDateFormat; 5 import java.util.Date; 6 import java.util.HashSet; 7 import java.util.Set; 8 import java.util.TreeSet; 9 10 import javax.persistence.*; 11 12 @Entity 13 @Table(name="JPATUNE_MOVIE") 14 @NamedQueries({ 15 @NamedQuery(name="Movie.findByTitle", query= 16 "select m from Movie m " + 17 "where lower(m.title) like :title") 18 }) 19 @SqlResultSetMappings({ 20 @SqlResultSetMapping(name="Movie.movieMapping", entities={ 21 @EntityResult(entityClass=Movie.class), 22 @EntityResult(entityClass=Director.class), 23 @EntityResult(entityClass=Person.class) 24 }), 25 @SqlResultSetMapping(name="Movie.movieMapping2", entities={ 26 @EntityResult(entityClass=Movie.class), 27 @EntityResult(entityClass=Director.class), 28 @EntityResult(entityClass=Person.class, fields={ 29 @FieldResult(name="id", column="p_id"), 30 @FieldResult(name="firstName", column="first_name"), 31 @FieldResult(name="lastName", column="last_name"), 32 @FieldResult(name="birthDate", column="birth_date") 33 }) 34 }) 35 }) 36 public class Movie implements Comparable<Movie>{ 37 @Id 38 @Column(name="ID", length=36) 39 private String id; 40 41 @Column(name="TITLE", length=32, nullable=false) 42 private String title; 43 44 @Temporal(TemporalType.DATE) 45 @Column(name="RELEASE_DATE") 46 private Date releaseDate; 47 48 @Column(name="RATING", length=6) 49 private String rating; 50 51 @Transient 52 private MovieRating mrating; 53 @PostLoad private void fromDB() { mrating=MovieRating.getFromMpaa(rating); } 54 55 @Column(name="MINUTES") 56 private Integer minutes; 57 58 @Column(name="PLOT", length=4000) 59 private String plot; 60 61 @ElementCollection(fetch=FetchType.LAZY) 62 @CollectionTable( 63 name="JPATUNE_MOVIEGENRE", 64 joinColumns=@JoinColumn(name="MOVIE_ID"), 65 uniqueConstraints=@UniqueConstraint(columnNames={"MOVIE_ID", "GENRE"})) 66 @Column(name="GENRE", length=20) 67 private Set<String> genres; 68 69 @ManyToOne(fetch=FetchType.LAZY, 70 cascade={CascadeType.PERSIST, CascadeType.DETACH}) 71 @JoinColumn(name="DIRECTOR_ID") 72 private Director director; 73 74 @OneToMany(fetch=FetchType.LAZY, mappedBy="movie", 75 cascade={CascadeType.PERSIST, CascadeType.DETACH, CascadeType.REMOVE}) 76 private Set<MovieRole> cast; 77 78 79 protected Movie() {} 80 public Movie(String id) { 81 this.id = id; 82 } 83 public Movie(String id, Integer minutes, String rating, Date releaseDate, String title) { 84 this.id=id; 85 this.minutes = minutes; 86 this.rating=rating; 87 this.mrating=MovieRating.getFromMpaa(rating); 88 this.releaseDate = releaseDate; 89 this.title=title; 90 } 91 92 public String getId() { return id; } 93 94 public String getTitle() { return title; } 95 public Movie setTitle(String title) { 96 this.title = title; 97 return this; 98 } 99 100 public Date getReleaseDate() { return releaseDate; } 101 public Movie setReleaseDate(Date releaseDate) { 102 this.releaseDate = releaseDate; 103 return this; 104 } 105 106 public MovieRating getRating() { return mrating; } 107 public Movie setRating(MovieRating rating) { 108 this.mrating = rating; 109 this.rating = rating==null ? null : rating.mpaa(); 110 return this; 111 } 112 113 public Integer getMinutes() { return minutes; } 114 public Movie setMinutes(Integer minutes) { 115 this.minutes = minutes; 116 return this; 117 } 118 119 public Set<String> getGenres() { 120 if (genres == null) { 121 genres = new TreeSet<String>(); 122 } 123 return genres; 124 } 125 public Movie setGenres(Set<String> genres) { 126 this.genres = genres; 127 return this; 128 } 129 public Movie addGenres(String...genre) { 130 if (genre!=null) { 131 for (String g : genre) { 132 getGenres().add(g); 133 } 134 } 135 return this; 136 } 137 138 public String getPlot() { return plot; } 139 public Movie setPlot(String plot) { 140 this.plot = plot; 141 return this; 142 } 143 144 public Director getDirector() { return director; } 145 public Movie setDirector(Director director) { 146 this.director = director; 147 return this; 148 } 149 150 public Set<MovieRole> getCast() { 151 if (cast==null) { 152 cast=new HashSet<MovieRole>(); 153 } 154 return cast; 155 } 156 protected void setCast(Set<MovieRole> cast) { 157 this.cast = cast; 158 } 159 public Movie addRole(MovieRole...role) { 160 if (role!=null) { 161 for (MovieRole r : role) { 162 r.setMovie(this); 163 getCast().add(r); 164 } 165 } 166 return this; 167 } 168 @Override 169 public int hashCode() { 170 return (director== null ? 0 : director.hashCode()) + 171 (minutes==null ? 0 : minutes) + 172 (rating==null? 0 : rating.hashCode()) + 173 (releaseDate==null? 0 : releaseDate.hashCode()) + 174 (title==null ? 0 : title.hashCode()); 175 } 176 177 @Override 178 public boolean equals(Object obj) { 179 if (this == obj) { return true; } 180 if (obj == null) { return false; } 181 Movie rhs = (Movie)obj; 182 183 if (title==null) { 184 if (rhs.title != null) { return false; } 185 } else if (!title.equals(rhs.title)) { 186 return false; 187 } 188 189 if (director==null) { 190 if (rhs.director != null) { return false; } 191 } else if (!director.equals(rhs.director)) { 192 return false; 193 } 194 195 if (releaseDate == null) { 196 if (rhs.releaseDate != null) { return false; } 197 } else if (!releaseDate.equals(rhs.releaseDate)) { 198 return false; 199 } 200 201 if (minutes == null) { 202 if (rhs.minutes != null) { return false; } 203 } else if (!minutes.equals(rhs.minutes)) { 204 return false; 205 } 206 207 if (rating == null) { 208 if (rhs.rating != null) { return false; } 209 } else if (!rating.equals(rhs.rating)) { 210 return false; 211 } 212 213 return true; 214 } 215 216 @Override 217 public int compareTo(Movie rhs) { 218 if (rhs == null) { return 1; } 219 220 if (title != null && rhs.title != null && title.compareTo(rhs.title)!=0) { 221 return title.compareTo(rhs.title); 222 } 223 if (releaseDate != null && rhs.releaseDate != null && releaseDate.compareTo(rhs.releaseDate)!=0) { 224 return releaseDate.compareTo(rhs.releaseDate); 225 } 226 return 0; 227 } 228 229 @Override 230 public String toString() { 231 DateFormat df = new SimpleDateFormat("yyyy"); 232 return title + (releaseDate==null ? "" : " (" + df.format(releaseDate) + ")"); 233 } 234 }