今天看了下Hibernate的@Column注解
@Column(
name="columnName"; 1
boolean unique() default false; 2
boolean nullable() default true; 3
boolean insertable() default true; 4
boolean updatable() default true; 5
String columnDefinition() default ""; 6
String table() default ""; 7
int length() default 255; 8
int precision() default 0; 9
int scale() default 0; 10
)
除了7、9、10,其他都容易理解。
其中9和10是设置decimal的精度,开始定义了Float和Double类型的字段,然后在这两种类型的字段上使用@Column注解,结果9和10不起作用。最后发现在使用BigDecimal类型时,9和10才起作用。如果自己使用columnDefinition定义类型(@Column(columnDefinition="decimal")),则需要自己定义精度,9和10是不起作用的,同理,使用Double和Float类型时MySQL中生成的是double和float,于是也不会起作用。
而7要和@SecondaryTable注解一起使用。
import java.math.BigDecimal;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.SecondaryTable;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
/**
* @author lan 2010-8-23
*/
@Entity
@Table(name="student")
@SecondaryTable(name="SName")
public class Student {
private Integer id;
private String name;
private Integer age;
private Date birthday;
private String resume;
private BigDecimal height;
private Double weight;
@Id
@GeneratedValue
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(table="SName")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Temporal(TemporalType.DATE)
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Lob
@Column(insertable=false)
public String getResume() {
return resume;
}
public void setResume(String resume) {
this.resume = resume;
}
@Column(precision=5,scale=2)
public BigDecimal getHeight() {
return height;
}
public void setHeight(BigDecimal height) {
this.height = height;
}
@Column(precision=5,scale=2)
public Double getWeight() {
return weight;
}
public void setWeight(Double weight) {
this.weight = weight;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + ", birthday=" + birthday + ", resume="
+ resume + ", height=" + height + ", weight=" + weight + "]";
}
}
本文只是作为自己的备忘录,本人对于hibernate的了解还很粗浅,欢迎各位大牛指教。