日期:2014-05-20  浏览次数:20647 次

hibernate怎么在类里用注解添加检查约束
这是我的代码
Java code

@Entity
@Table(name="DB_Student")
@SequenceGenerator(name="stu_seq",sequenceName="stu_seq",initialValue=1,allocationSize=1)
public class Student {
    private int id;
    private String name;
    private Sex sex;
    private int age;
    private Date birthday;
    private boolean statu;
    
    @Id
    @Column(name="stu_id")
    @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="stu_seq")
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
    @Enumerated(EnumType.STRING)
    public Sex getSex() {
        return sex;
    }
    public void setSex(Sex sex) {
        this.sex = sex;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    @Column(nullable=true)
    public boolean isStatu() {
        return statu;
    }
    public void setStatu(boolean statu) {
        this.statu = statu;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}



请问如果我要用注解把年龄添加一个检查约束 age>0 and age<100 改怎么写

------解决方案--------------------
http://www.ibm.com/developerworks/cn/java/j-hibval.html
------解决方案--------------------
@Range(min = 10, max = 30)

10-30岁