Hibernate二 映射 注解 一级缓存

Hibernate映射
1.@Entity
被该注解修饰的POJO类是一个实体,可以用name属性指定该实体类的名称,系统默认以该类的类名作为实体类的名称。
2.@Table
指定持久化类所映射的表,它的属性包括:
catalog:将表放入指定的catalog中,如果没有指定,则放入默认的catalog中。
name:设置表名。
schema:将表放入指定的schema中,如果没有指定,则放入默认的schema中。
indexes:为持久化类所映射的表设置索引,该属性的值是一个@Index注解数组。包括columnList:设置对哪些列建立索引,name:该索引的名字,unique:该索引是否具有唯一性
uniqueConstraints:设置表的唯一约束
3.@Access
该注解的value属性支持:
AccessType.PROPERTY 使用getter/setter方法访问属性(默认)
AccessType.FIELD 直接通过成员变量来访问属性(不建议)
4.@Column
用于指定某个属性所映射的数据列的详细信息,包括以下常用属性:
name:指定列名
length:指定数据的最大长度,默认值:255
unique:指定该列书否具有唯一约束
insertable:指定该列是否包含在Hibernate生成的insert语句的列列表中,默认值为true
updatable:指定该列是否包含在Hibernate生成的update语句的列列表中,默认值为true
nullable:该列是否允许为null
precision:当列为decimal类型时,该列支持的最大有效数字位数
scale:当列为decimal类型时,该列支持最大支持的小数位数
columnDefinition:该属性的值是一个代表列定义的SQL字符串,指定创建该数据列的SQL语句
5.@Temporal
数据库中表示日期、时间的类型比较多,使用@Temporal指定类型,其value值包括:
TemporalType.DATE TemporalType.TIME TemporalType.TIMESTAMP
6.@Lob,@Basic修饰大数据类型的属性
使用数据库保存图片或者大段文章时,数据库通常会采用Blob,Clob类型的数据列来保存。在持久化类中只要使用@Lob即可。
如:
@Entity
@Table(name="t_person")
public class Person {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@Column(name="t_name")
private String name;
@Lob
@Basic(fetch=FetchType.LAZY) //延迟加载,只有在真正需要时才从数据表中加载数据
private byte[] pic;
}
public class test {
public static void main(String[] args) {
Configuration config=new Configuration().configure();
SessionFactory factory=config.buildSessionFactory();
Session session=factory.openSession();
Transaction tx=session.beginTransaction();
Person p=new Person();
p.setName("lyy");
File file=new File("logo.jpg");
byte[] content=new byte[(int)file.length()];
new FileInputStream(file).read(content);
person.setPic(content);
session.save(p);
tx.commit();
session.close();
factory.close();
}
}
6.映射主键
@Id修饰属性,注解时无需指定任何属性
指定主键生成策略:使用@GeneratedValue来修饰,它有以下属性:
(1)strategy:包括
GenerationType.AUTO 自动选择最适合底层数据库的主键生成策略,默认值
GenerationType.IDENTITY 对于MySQL,SQL Server这样的数据库,选择自增长的主键生成策略
GenerationType.SEQUENCE 对于Oracle这样的数据库,选择基于Sequence的主键生成策略,应与@SequenceGenerator一起使用
GenerationType.TABLE 使用辅助表来生成主键,应与@TableGenerator一起使用
(2)generator
使用GenerationType.SEQUENCE和GenerationType.TABLE时,该属性引用@SequenceGenerator,@TableGenerator所定义的生成器的名称
@SequenceGenerator属性包括:
name:主键生成器的名称
allocationSize:底层Sequence每次生成主键值的个数
catalog:将底层Sequence放入指定catalog中
schema:将底层Sequence放入指定schema中
initialValue:底层Sequence的初始值
sequenceName:底层Sequence的名称


Session一级缓存
一级缓存生命周期很短,与session生命周期一致,所以一级缓存也叫session级缓存或事务级缓存。位于一级缓存中的对象处于持久化状态,它与表中的相关记录对应,session能够在某些时间点,按照缓存中持久化对象的属性变化来同步数据库中表的记录,这一过程称为清理缓存。
实现原理:Session缓存是由它的实现类SessionImpl中定义的一些集合属性构成的,原理是保证有一个引用在关联着某个持久化对象,保持它的生命周期不会结束。
作用:减少数据库访问,从内存中取数据比数据库中要快得多。缓存中的数据与数据库中的同步:缓存会把改变的sql语句合并,减少访问次数。缓存中的对象存在循环关联时,session会保证不出现访问对象图的死循环。
将test部分改为:
@Test
public void test() {
Person p=(Person)session.get(Person.class, 4);
System.out.println(p);
Person p2=(Person)session.get(Person.class, 4);
System.out.println(p2);
}
Hibernate: select person0_.id as id1_0_0_, person0_.name as name2_0_0_, person0_.password as password3_0_0_, person0_.birthday as birthday4_0_0_, (select count(*) from t_person) as formula0_0_ from t_person person0_ where person0_.id=?
Person [id=4, name=ert, password=0, birthday=null]
Person [id=4, name=ert, password=0, birthday=null]
只查询一次,第二次查询时会直接先在缓存中找,若找到,就不去数据库中查找了
若改为:
@Test
public void test() {
Person p=(Person)session.get(Person.class, 4);
System.out.println(p);
session.clear();
Person p2=(Person)session.get(Person.class, 4);
System.out.println(p2);
}
Hibernate: select person0_.id as id1_0_0_, person0_.name as name2_0_0_, person0_.password as password3_0_0_, person0_.birthday as birthday4_0_0_, (select count(*) from t_person) as formula0_0_ from t_person person0_ where person0_.id=?
Person [id=4, name=ert, password=0, birthday=null]
Hibernate: select person0_.id as id1_0_0_, person0_.name as name2_0_0_, person0_.password as password3_0_0_, person0_.birthday as birthday4_0_0_, (select count(*) from t_person) as formula0_0_ from t_person person0_ where person0_.id=?
Person [id=4, name=ert, password=0, birthday=null]
session会缓存通过它获得的持久化对象,如果在同一个session中获得用一条记录多次,将只会发起一条sql语句,其余的都是在缓存中直接返回。
session.evict(p); 只清除p的缓存
在进行批量插入时,缓存中数据量就会很大
@Test
public void test() {
for(int i=0;i<1000;i++){
Person p =new Person("admin"+i,1234+i,new Date());
session.persist(p);
System.out.println(p);
}
}
修改为:
@Test
public void test() {
for(int i=0;i<1000;i++){
Person p =new Person("admin"+i,1234+i,new Date());
session.persist(p);
if(i%10==0){
session.flush(); //可以立即同步持久化状态数据到数据库表
session.clear();
}
System.out.println(p);
}
tx.commit();
}

原文地址:https://www.cnblogs.com/lyy-2016/p/5705327.html