hibernate-annotations——@Column

今天看了下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的了解还很粗浅,欢迎各位大牛指教。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值