在hibernate中的对象有三种状态,异同比较
(关键在于:有没有id,id在数据库中有没有,在内存中有没有【Session缓存】)
Transient: new出来的新对象,没有id
(内存中的对象,没有id,缓存没有)
Persistent:Transient对象save后有id了,保存后的对象
(内存中有,缓存中有,有id,数据库有)
Detached:session关闭后的对象
(内存有,缓存(Session)没有,数据库有,有id)
例子(我用的是bbs的版块对象:id,标题)
/**
* Transient: new出来的新对象,没有id
*/
Dbforum f = new Dbforum();
f.setFname("感情世界");
Session s = sessionFactory.openSession();
s.save(f);
/**
* Persistent:Transient对象save后有id了,保存后的对象
*/
System.out.println(f.getFid());
s.beginTransaction().commit();
/**
* Detached:session关闭后的对象
*/
System.out.println(f.getFid());
hibernate二级缓存的配置(其中的一种)
1、hibernate.cfg.xml
<property name="cache.use_second_level_cache">true</property>
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
2、导入jar包
hibernate自带的缓存接口
Apache的Commons logging 日志文件
3、pojo配置(我用的Annotation注释)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)