View Javadoc
1   package myorg.entityex.annotated;
2   
3   import javax.persistence.Access;
4   import javax.persistence.AccessType;
5   import javax.persistence.Column;
6   import javax.persistence.Entity;
7   import javax.persistence.EnumType;
8   import javax.persistence.Enumerated;
9   import javax.persistence.GeneratedValue;
10  import javax.persistence.Id;
11  import javax.persistence.Table;
12  import javax.persistence.Transient;
13  
14  @Entity
15  @Table(name="ENTITYEX_DOG")
16  @Access(AccessType.FIELD)
17  public class Dog {
18  	public enum Sex {
19  		MALE, FEMALE
20  	}
21  	public enum Color {
22  		WHITE, BLACK, BROWN, MIX 
23  	}
24  	public enum Breed {
25  		LABRADOR("Lab"),
26  		SAINT_BERNARD("Saint Bernard");
27  		public final String prettyName;
28  		private Breed(String prettyName) { this.prettyName = prettyName; }
29  		
30  		public static Breed getBreed(String prettyName) {
31  			for (Breed breed : values()) {
32  				if (breed.prettyName.equals(prettyName)) {
33  					return breed;
34  				}
35  			}
36  			return null;
37  		}
38  	}
39  	
40  	@Id @GeneratedValue
41  	private int id;
42  	@Enumerated(EnumType.ORDINAL)
43  	private Sex gender;
44  	@Enumerated(EnumType.STRING)
45  	@Column(length=16)
46  	private Color color;
47  	@Transient
48  	private Breed breed;
49  	
50  	public int getId() { return id; }
51  	public void setId(int id) {
52  		this.id = id;
53  	}
54  
55  	public Sex getGender() { return gender; }
56  	public Dog setGender(Sex gender) {
57  		this.gender = gender;
58  		return this;
59  	}
60  
61  	public Color getColor() { return color; }
62  	public Dog setColor(Color color) {
63  		this.color = color;
64  		return this;
65  	}
66  
67  	public Breed getBreed() {
68  		return breed;
69  	}
70  	public Dog setBreed(Breed breed) {
71  		this.breed = breed;
72  		return this;
73  	}
74  	
75  	@Access(AccessType.PROPERTY)
76  	@Column(name="BREED", length=32)
77  	protected String getDBBreed() {
78  		return breed==null ? null : breed.prettyName;
79  	}
80  	protected void setDBBreed(String dbValue) {
81  		breed=Breed.getBreed(dbValue);
82  	}
83  }