1 package ejava.jpa.examples.tuning.bo; 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="JPATUNE_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 String getModName() { return person==null?null : person.getModName(); } 34 public Date getBirthDate() { return person==null?null : person.getBirthDate(); } 35 public Director setFirstName(String name) { if (person!=null){ person.setFirstName(name);} return this;} 36 public Director setLastName(String name) { if (person!=null){ person.setLastName(name);} return this;} 37 public Director setModName(String name) { if (person!=null){ person.setModName(name);} return this;} 38 public Director setBirthDate(Date date) { if (person!=null){ person.setBirthDate(date);} return this;} 39 40 public Set<Movie> getMovies() { 41 if (movies == null) { 42 movies = new TreeSet<Movie>(); 43 } 44 return movies; 45 } 46 protected void setMovies(Set<Movie> movies) { 47 this.movies = movies; 48 } 49 public Director addMovie(Movie...movie) { 50 if (movie != null) { 51 for (Movie m : movie) { 52 getMovies().add(m); 53 } 54 } 55 return this; 56 } 57 58 @Override 59 public int hashCode() { 60 return (person==null ? 0 : person.hashCode()); 61 } 62 63 @Override 64 public boolean equals(Object obj) { 65 try { 66 if (this==obj) { return true; } 67 if (obj==null) { return false; } 68 Director rhs = (Director)obj; 69 if (person == null) { 70 if (rhs.person != null) { return false; } 71 } else if (!person.equals(rhs.person)) { 72 return false; 73 } 74 return true; 75 } catch (Exception ex) { return false; } 76 } 77 78 @Override 79 public String toString() { 80 return person.toString(); 81 } 82 }