1 package myorg.queryex; 2 3 import java.util.Comparator; 4 5 import java.util.Date; 6 import java.util.Set; 7 import java.util.TreeSet; 8 9 import javax.persistence.CascadeType; 10 import javax.persistence.Entity; 11 import javax.persistence.FetchType; 12 import javax.persistence.Id; 13 import javax.persistence.JoinColumn; 14 import javax.persistence.MapsId; 15 import javax.persistence.OneToMany; 16 import javax.persistence.OneToOne; 17 import javax.persistence.Table; 18 import javax.persistence.Version; 19 20 @Entity 21 @Table(name="QUERYEX_ACTOR") 22 public class Actor { 23 @Id 24 private String id; 25 @Version 26 private int version; 27 28 @OneToOne(optional=false, fetch=FetchType.EAGER, 29 cascade={CascadeType.PERSIST, CascadeType.DETACH}) 30 @MapsId 31 @JoinColumn(name="PERSON_ID") 32 private Person person; 33 34 @OneToMany(mappedBy="actor", 35 cascade={CascadeType.PERSIST, CascadeType.REMOVE}) 36 private Set<MovieRole> roles= new TreeSet<MovieRole>(new Comparator<MovieRole>() { 37 @Override 38 public int compare(MovieRole lhs, MovieRole rhs) { 39 if (lhs.getMovie() != null && rhs.getMovie() != null) { 40 return lhs.getMovie().compareTo(rhs.getMovie()); 41 } 42 return 0; 43 } 44 }); 45 46 protected Actor() {} 47 public Actor(Person person) { 48 this.person = person; 49 } 50 51 public Person getPerson() { return person; } 52 public String getFirstName() { return person==null?null : person.getFirstName(); } 53 public String getLastName() { return person==null?null : person.getLastName(); } 54 public Date getBirthDate() { return person==null?null : person.getBirthDate(); } 55 public Actor setFirstName(String name) { if (person!=null){ person.setFirstName(name);} return this;} 56 public Actor setLastName(String name) { if (person!=null){ person.setLastName(name);} return this;} 57 public Actor setBirthDate(Date date) { if (person!=null){ person.setBirthDate(date);} return this;} 58 public int getVersion() { return version; } 59 60 public Set<MovieRole> getRoles() { 61 return roles; 62 } 63 64 public void setRoles(Set<MovieRole> roles) { 65 this.roles = roles; 66 } 67 68 @Override 69 public int hashCode() { 70 return (person==null ? 0 : person.hashCode()); 71 } 72 73 @Override 74 public boolean equals(Object obj) { 75 try { 76 if (this==obj) { return true; } 77 if (obj==null) { return false; } 78 Actor rhs = (Actor)obj; 79 if (person == null) { 80 if (rhs.person != null) { return false; } 81 } else if (!person.equals(rhs.person)) { 82 return false; 83 } 84 return true; 85 } catch (Exception ex) { return false; } 86 } 87 88 @Override 89 public String toString() { 90 return person.toString() + ", version=" + version; 91 } 92 }