当持久化类的属性为大字段时,String类型映射到数据库中最大的长度为255,满足不了需求,应该通过@Lob注解标注在String类型属性的get方法上,表示映射到数据库中为大文本类型;@Lob注解也可用于Byte[]类型,映射到数据库中为存放二进制数据类型
默认情况下,持久化类中的属性都会映射到数据库中。如果不想让持久化类中的某个属性映射到数据库,通过@Transient注解标注在属性的get方法上,表示该属性不被映射到数据库中。
如果在加载一个持久化对象时,不想得到这个对象的大文本属性,可以通过@Basic(fetch = FetchType.LAZY)来实现,表示懒加载,查询对象时不查询该属性,只有在访问这个属性时才去查询数据库。
package cn.com.jyp.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
@Entity
@Table(name = "person")
public class Person {
private Integer id;
private String introduction;// 简介
private Byte[] file;
@Id
@GeneratedValue
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Lob
public String getIntroduction() {
return introduction;
}
public void setIntroduction(String introduction) {
this.introduction = introduction;
}
@Lob
public Byte[] getFile() {
return file;
}
public void setFile(Byte[] file) {
this.file = file;
}
}
默认情况下,持久化类中的属性都会映射到数据库中。如果不想让持久化类中的某个属性映射到数据库,通过@Transient注解标注在属性的get方法上,表示该属性不被映射到数据库中。
package cn.com.jyp.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
@Entity
@Table(name = "person")
public class Person {
private Integer id;
private String name;
private String address;
@Id
@GeneratedValue
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Transient
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
如果在加载一个持久化对象时,不想得到这个对象的大文本属性,可以通过@Basic(fetch = FetchType.LAZY)来实现,表示懒加载,查询对象时不查询该属性,只有在访问这个属性时才去查询数据库。
package cn.com.jyp.model;
import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
@Entity
@Table(name = "person")
public class Person {
private Integer id;
private String name;
private String introduction;// 简介
@Id
@GeneratedValue
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Lob
@Basic(fetch = FetchType.LAZY)
public String getIntroduction() {
return introduction;
}
public void setIntroduction(String introduction) {
this.introduction = introduction;
}
}