View Javadoc
1   package ejava.jpa.examples.tuning.bo;
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  
19  @Entity
20  @Table(name="JPATUNE_ACTOR")
21  public class Actor {
22  	@Id
23  	private String id;
24  
25  	@OneToOne(optional=false, fetch=FetchType.EAGER,
26  			cascade={CascadeType.PERSIST, CascadeType.DETACH})
27  	@MapsId
28  	@JoinColumn(name="PERSON_ID")
29  	private Person person;
30  	
31  	@OneToMany(mappedBy="actor", 
32  			cascade={CascadeType.PERSIST, CascadeType.REMOVE})
33  	private Set<MovieRole> roles= new TreeSet<MovieRole>(new Comparator<MovieRole>() {
34  		@Override
35  		public int compare(MovieRole lhs, MovieRole rhs) {
36  			if (lhs.getMovie() != null && rhs.getMovie() != null) {
37  				return lhs.getMovie().compareTo(rhs.getMovie());
38  			}
39  			return 0;
40  		}
41  	});
42  
43  	protected Actor() {}
44  	public Actor(Person person) {
45  		this.person = person;
46  	}
47  	
48  	public Person getPerson() { return person; }
49  	public String getFirstName() { return person==null?null : person.getFirstName(); }
50  	public String getLastName() { return person==null?null : person.getLastName(); }
51  	public String getModName() { return person==null?null : person.getModName(); }
52  	public Date getBirthDate() { return person==null?null : person.getBirthDate(); }
53  	public Actor setFirstName(String name) { if (person!=null){ person.setFirstName(name);} return this;}
54  	public Actor setLastName(String name) { if (person!=null){ person.setLastName(name);} return this;}
55  	public Actor setModName(String name) { if (person!=null){ person.setModName(name);} return this;}
56  	public Actor setBirthDate(Date date) { if (person!=null){ person.setBirthDate(date);} return this;}
57  
58  	public Set<MovieRole> getRoles() {
59  		return roles;
60  	}
61  
62  	public void setRoles(Set<MovieRole> roles) {
63  		this.roles = roles;
64  	}
65  	
66  	@Override
67  	public int hashCode() {
68  		return (person==null ? 0 : person.hashCode());
69  	}
70  	
71  	@Override
72  	public boolean equals(Object obj) {
73  		try { 
74  			if (this==obj) { return true; }
75  			if (obj==null) { return false; }
76  			Actor rhs = (Actor)obj;
77  			if (person == null) {
78  				if (rhs.person != null) { return false; }
79  			} else if (!person.equals(rhs.person)) {
80  				return false;
81  			}
82  			return true;
83  		} catch (Exception ex) { return false; }
84  	}
85  	
86  	@Override
87  	public String toString() {
88  		return person.toString(); 
89  	}
90  }