使用@注解定义在POJO类上定义属性时,教程上都是在field上使用@注解,那是为了方便演示学习理解。
正常编写程序时,应该把@注解写到getter方法上,这样一眼就能看清该 POJO类有多少属性,不然就很混乱。
例如:
@Entity
@Table (name="tb_cat")
public class Cat{
private Integer id;
private String name;
private Date birthday;
private java.util.Date createDate;
private double salary;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name="name", nullable=true, length=50)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Temporal(TemporalType.TIMESTAMP)
public java.util.Date getCreateDate() {
return createDate;
}
public void setCreateDate(java.util.Date createDate) {
this.createDate = createDate;
}
}
}