参考资料:
hibernate 3.2 帮助文档 19.2节
1.在hibernate的属性文件中配置sessionFactory的属性,
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.use_sql_comments">${hibernate.use_sql_comments}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
//启用二级缓存
<prop key="hibernate.cache.use_second_level_cache">true</prop>
//ehcache的配置文件
<prop key="hibernate.cache.configurationResourceName">ehcache.xml</prop>
//启用查询缓存
<prop key="hibernate.cache.use_query_cache">true</prop>
//指定hibernate缓存实现为 ehcache
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
</props>
</property>
2.ehcache.xml
<?xml version="1.0" encoding="UTF-8" ?>
<ehcache>
<defaultCache maxElementsInMemory="10000" eternal="true" overflowToDisk="false" timeToIdleSeconds="300" timeToLiveSeconds="180" />
</ehcache>
3.使用注解配置实体
@Entity
@Table(name="T1_YP_LB")
//在实体上启用缓存 org.hibernate.annotations.CacheConcurrencyStrategy指定缓存的模式
@Cache(usage=org.hibernate.annotations.CacheConcurrencyStrategy.READ_ONLY)
public class YP_lb{
@OneToMany(mappedBy="yplb",fetch=FetchType.EAGER)
fetch
fetch 属性是 FetchType 类型的属性。可选择项包括:FetchType.EAGER 和 FetchType.LAZY。前者表示关联关系的从类在主类加载的时候同时加载,后者表示关联关
系的从类在自己被访问时才加载。默认值是 FetchType.EAGER。
@OneToMany默认类型为FetchType.LAZY
//在一对多的关系式指定缓存类别,这里说明一下虽然在实体上使用了缓存但是只会缓存实体的普通属性,对于集合要有自己的缓存区域
//指定lazy=false 当lazy=true时可能造成实体无法被缓存
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE,include="non-lazy")
public Set<YP_YLFL> getYlfls() {
return ylfls;
}
}
@Entity
@Table(name="T1_YP_YLFL_2")
@Cache(usage=org.hibernate.annotations.CacheConcurrencyStrategy.READ_ONLY)
public class YP_YLFL_2 implements Serializable {
//fetch 指定为eager标识使用lazy
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="FLID")
public YP_YLFL getFlid() {
return flid;
}
}
4.
数据库dao类的操作
public List<YP_lb> findAll() throws DataAccessException {
String queryString = "from YP_lb";
return (List<YP_lb>)hibernateTemplate.execute(new HibernateCallback() {
public List<YP_lb> doInHibernate(Session session) throws HibernateException,
SQLException {
Query query=session.createQuery("from YP_lb lb");
//启用查询缓存
query.setCacheable(true);
List<YP_lb> lb=query.list();
return lb;
}
});
}
分享到:
相关推荐
hiberante-4.2.3-part4
hiberante-4.2.3-part3
hiberante4.2.3-part01
hiberante4.2.3-part2
Struts2+Hiberante+ajax+三级联动,提供完整的JAR包和MYSQL数据库文件
NULL 博文链接:https://zgdkik.iteye.com/blog/1835856
hiberante3 注解帮助文档hiberante3 注解帮助文档hiberante3 注解帮助文档hiberante3 注解帮助文档hiberante3 注解帮助文档
DAO层中对Hiberante实例,大家交流一下
Hiberante3.jar + API
本Demo采用Struts2.1.8+Spring2.5.6(Annotation注解)+Hiberante3.3.2(Annotation注解)+ExtJS(Struts2-json)
最新springboot2基础hiberante5完整项目,打包jar,运行jsp,包括后台与前台,拦截器,登录,后台下载就可以使用,注意不是jpa,里面有完整Dao,千万级数据项目分离的代码,为了适合老项目开发特意集成hiberante5....
hibernate 源码 和hibernate配置的html文档。。非常全面的介绍了hibernate配置。分别在src和zh-cn文件中
hiberante查询方式使用详解,有hql,sql,qbc,以及存储过程,hiberante的基础查询
spring整合
Hibernate Tools是一套全新而且完整的面向Hibernate3的工具集合,它包含了Eclipse插件和Ant编译流程。Hibernate Tools是JBoss Tools的核心组件,所以他也是JBoss Developer Studio的一部分
Hiberante3_HQL,Hibernate3_缓存机制,Hibernate3_数据加载
Hiberante HTML 帮助文档 Hiberante HTML 帮助文档 Hiberante HTML 帮助文档
hibernate5.0.7jar包,主要是jar包,方便大家下载使用。主要是jar包,方便大家下载使用。主要是jar包,方便大家下载使用
Struts2+hiberante+Spring+Extjs+OA办公系统,完整包正常运行