1 package myorg.queryex; 2 3 import java.util.Date; 4 import java.util.Set; 5 import java.util.TreeSet; 6 7 import javax.persistence.*; 8 9 @Entity 10 @Table(name="QUERYEX_DIRECTOR") 11 public class Director { 12 @Id 13 private String id; 14 15 @OneToOne(optional=false, fetch=FetchType.EAGER, 16 cascade={CascadeType.PERSIST, CascadeType.DETACH}) 17 @JoinColumn(name="PERSON_ID") 18 @MapsId 19 private Person person; 20 21 @OneToMany(mappedBy="director", 22 cascade={CascadeType.PERSIST, CascadeType.DETACH}) 23 private Set<Movie> movies; 24 25 protected Director() {} 26 public Director(Person person) { 27 this.person = person; 28 } 29 30 public Person getPerson() { return person; } 31 public String getFirstName() { return person==null?null : person.getFirstName(); } 32 public String getLastName() { return person==null?null : person.getLastName(); } 33 public Date getBirthDate() { return person==null?null : person.getBirthDate(); } 34 public Director setFirstName(String name) { if (person!=null){ person.setFirstName(name);} return this;} 35 public Director setLastName(String name) { if (person!=null){ person.setLastName(name);} return this;} 36 public Director setBirthDate(Date date) { if (person!=null){ person.setBirthDate(date);} return this;} 37 38 public Set<Movie> getMovies() { 39 if (movies == null) { 40 movies = new TreeSet<Movie>(); 41 } 42 return movies; 43 } 44 protected void setMovies(Set<Movie> movies) { 45 this.movies = movies; 46 } 47 public Director addMovie(Movie...movie) { 48 if (movie != null) { 49 for (Movie m : movie) { 50 getMovies().add(m); 51 } 52 } 53 return this; 54 } 55 56 @Override 57 public int hashCode() { 58 return (person==null ? 0 : person.hashCode()); 59 } 60 61 @Override 62 public boolean equals(Object obj) { 63 try { 64 if (this==obj) { return true; } 65 if (obj==null) { return false; } 66 Director rhs = (Director)obj; 67 if (person == null) { 68 if (rhs.getPerson() != null) { return false; } 69 } else if (!person.equals(rhs.getPerson())) { 70 return false; 71 } 72 return true; 73 } catch (Exception ex) { return false; } 74 } 75 76 @Override 77 public String toString() { 78 return person.toString(); 79 } 80 }