1、修改Person.java中的代码
package cn.sunft.bean;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
@Entity
@Table(name="person")
public class Person {
private Integer id;
private String name;
private Date birthday = new Date();//1987-12-10
private Gender gender = Gender.MAN;//设置默认属性
private String info; //大段文本信息
private Byte[] file; //大段二进制数据
private String imagepth; //希望该属性不成为持久化字段
@Transient//该字段不与数据库进行映射
public String getImagepth() {
return imagepth;
}
public void setImagepth(String imagepth) {
this.imagepth = imagepth;
}
@Lob//针对二进制文本的配置
@Basic(fetch=FetchType.LAZY) //指定延迟加载
public Byte[] getFile() {
return file;
}
public void setFile(Byte[] file) {
this.file = file;
}
@Lob//针对大段文本的注解
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
//@Enumerated(EnumType.ORDINAL)//保存索引到数据库中
@Enumerated(EnumType.STRING)//保存字面值到数据库
@Column(length=5, nullable=false)//一定要加上非空约束
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
@Temporal(TemporalType.DATE)
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Person() {
super();
}
public Person(String name) {
super();
this.name = name;
}
//也可以直接标注在属性上
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
//指定列的长度
@Column(length=10, nullable=false,name="personName")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2、运行PersonTest.java中的save()方法(参见JPA第二篇博客)之后生成的数据库表结构