1 package ejava.jpa.examples.tuning.bo; 2 3 import java.util.Date; 4 5 import javax.persistence.*; 6 7 @Entity 8 @Table(name="JPATUNE_PERSON", 9 uniqueConstraints={ 10 @UniqueConstraint(name="PERSON_NAMES_UNIQUE", columnNames={"LAST_NAME","FIRST_NAME","MOD_NAME"}) 11 }) 12 public class Person { 13 @Id 14 @Column(name="ID", length=36) 15 private String id; 16 17 @Column(name="FIRST_NAME", length=128) 18 private String firstName; 19 20 @Column(name="LAST_NAME", length=128, nullable=false) 21 private String lastName; 22 23 @Column(name="MOD_NAME", length=32) 24 private String modName; 25 26 @Temporal(TemporalType.DATE) 27 @Column(name="BIRTH_DATE") 28 private Date birthDate; 29 30 protected Person() {} 31 public Person(String id) { this.id = id; } 32 33 public String getId() { 34 return id; 35 } 36 public String getFirstName() { return firstName; } 37 public Person setFirstName(String firstName) { 38 this.firstName = firstName; 39 return this; 40 } 41 42 public String getLastName() { return lastName; } 43 public Person setLastName(String lastName) { 44 this.lastName = lastName; 45 return this; 46 } 47 48 public String getModName() { return modName; } 49 public Person setModName(String modName) { 50 this.modName = modName; 51 return this; 52 } 53 54 public Date getBirthDate() { return birthDate; } 55 public Person setBirthDate(Date birthDate) { 56 this.birthDate = birthDate; 57 return this; 58 } 59 60 @Override 61 public int hashCode() { 62 return (firstName==null?0:firstName.hashCode()) + 63 (lastName==null?0:lastName.hashCode()) + 64 (modName==null?0:modName.hashCode()) + 65 (birthDate==null?0:birthDate.hashCode()); 66 } 67 68 @Override 69 public boolean equals(Object obj) { 70 try { 71 if (this == obj) { return true; } 72 if (obj==null) { return false; } 73 Person rhs = (Person)obj; 74 if (firstName!=null && lastName!=null && modName!=null && birthDate!=null) { 75 return firstName.equals(rhs.firstName) && 76 lastName.equals(rhs.lastName) && 77 modName.equals(rhs.modName) && 78 birthDate.equals(rhs.birthDate) && 79 id==null?true:id.equals(rhs.id); 80 } 81 if ((firstName==null && rhs.firstName!=null) || 82 (lastName==null && rhs.lastName!=null) || 83 (modName==null && rhs.lastName!=null) || 84 (birthDate==null && rhs.birthDate!=null)) { 85 return false; 86 } 87 return id==null?true:id.equals(rhs.id); 88 } catch (Exception ex) { return false; } 89 } 90 91 @Override 92 public String toString() { 93 StringBuilder text = new StringBuilder(); 94 if (firstName != null) { text.append(firstName); } 95 if (firstName != null && lastName!=null) { text.append(" "); } 96 if (lastName != null) { text.append(lastName); } 97 if (modName != null) { text.append(" (").append(modName).append(")"); } 98 return text.toString(); 99 } 100 101 }