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