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