Skip to content

Instantly share code, notes, and snippets.

@stliu
Created July 1, 2011 05:19
Show Gist options
  • Save stliu/1057915 to your computer and use it in GitHub Desktop.
Save stliu/1057915 to your computer and use it in GitHub Desktop.
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
/**
* @author Strong Liu
*/
@Entity
public class Cat {
@Id
@GeneratedValue
private Long id; // identifier
@Temporal( TemporalType.TIMESTAMP )
private Date birthdate;
@Enumerated
private Color color;
private char sex;
private float weight;
private int litterId;
@OneToMany(mappedBy = "kittens")
private Cat mother;
@ManyToOne
private Set<Cat> kittens = new HashSet<Cat>();
private void setId(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
void setBirthdate(Date date) {
birthdate = date;
}
public Date getBirthdate() {
return birthdate;
}
void setWeight(float weight) {
this.weight = weight;
}
public float getWeight() {
return weight;
}
public Color getColor() {
return color;
}
void setColor(Color color) {
this.color = color;
}
void setSex(char sex) {
this.sex = sex;
}
public char getSex() {
return sex;
}
void setLitterId(int id) {
this.litterId = id;
}
public int getLitterId() {
return litterId;
}
void setMother(Cat mother) {
this.mother = mother;
}
public Cat getMother() {
return mother;
}
void setKittens(Set<Cat> kittens) {
this.kittens = kittens;
}
public Set<Cat> getKittens() {
return kittens;
}
// addKitten not needed by Hibernate
public void addKitten(Cat kitten) {
kitten.setMother( this );
kitten.setLitterId( kittens.size() );
kittens.add( kitten );
}
}
import javax.persistence.Entity;
/**
* @author Strong Liu
*/
@Entity
public class DomesticCat extends Cat {
private String name;
public String getName() {
return name;
}
protected void setName(String name) {
this.name = name;
}
}
/**
* @author Strong Liu
*/
public enum Color {
TABBY,BLACK,GINGER;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment