1 package myorg.queryex; 2 3 import javax.persistence.*; 4 5 @Entity 6 @Table(name="QUERYEX_MOVIEROLE") 7 @IdClass(MovieRolePK.class) 8 public class MovieRole { 9 @Id 10 @ManyToOne(optional=false, fetch=FetchType.LAZY) 11 @JoinColumn(name="MOVIE_ID") 12 private Movie movie; 13 14 @Id 15 @Column(name="MOVIE_ROLE", length=32) 16 private String role; 17 18 @ManyToOne(optional=false, fetch=FetchType.EAGER, 19 cascade={CascadeType.DETACH}) 20 @JoinColumn(name="ACTOR_ID") 21 private Actor actor; 22 23 public MovieRole() {} 24 public MovieRole(String role) { this(null, role); } 25 public MovieRole(Movie movie, String role) { 26 this.movie = movie; 27 this.role = role; 28 } 29 30 public Movie getMovie() { return movie; } 31 MovieRole setMovie(Movie movie) { this.movie=movie; return this; } 32 public String getRole() { return role; } 33 34 public Actor getActor() { return actor; } 35 public MovieRole setActor(Actor actor) { 36 this.actor = actor; 37 return this; 38 } 39 40 @Override 41 public int hashCode() { 42 return (role==null?0:role.hashCode()) + (actor==null?0:actor.hashCode()); 43 } 44 45 @Override 46 public boolean equals(Object obj) { 47 try { 48 if (this == obj) { return true; } 49 if (obj == null) { return false; } 50 MovieRole rhs = (MovieRole)obj; 51 if (role==null && rhs.role!=null) { return false; } 52 if (actor==null && rhs.actor!=null) { return false; } 53 return role.equals(rhs.role) && actor.equals(rhs.actor); 54 } catch (Exception ex) { return false; } 55 } 56 57 @Override 58 public String toString() { 59 StringBuilder text = new StringBuilder() 60 .append(movie.getTitle()) 61 .append(" ") 62 .append(role); 63 if (actor != null) { 64 text.append(" ").append(actor); 65 } 66 return text.toString(); 67 } 68 }