- 浏览: 245444 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
javatozhang:
讲解的的确不错。很实用。
Hibernate数据拦截与事件监听 -
sjx19871109:
更正一个地方:<event type="pos ...
Hibernate search -
xifan:
你好,楼主。
mutable="false 好像是 ...
Hibernate持久化对象生命周期 -
leo_cao:
很好,很实用
Hibernate数据拦截与事件监听 -
fehly:
47816778 写道你这样不会出现number 的精度问题吗 ...
Hibernate Annotations
使用dynamic-insert与dynamic-update
动态的添加与更新
1)<property>元素 insert属性:设置为false,在insert语句中不包含这个字段,表示永远不会被插入,默认true
2)<property>元素 update属性:设置为false,在update语句中不包含这个字段,表示永远不会被修改,默认true
3)<class>元素 mutable属性:设置为false就是把所有的<property>元素的update属性设置为了false,说明这个对象不会被更新,默认true
4)<property>元素 dynamic-insert属性:设置为true,表示insert对象的时候,生成动态的insert语句,如果这个字段的值是null就不会加入到insert语句当中.默认false
5)<property>元素 dynamic-update属性,设置为true,表示update对象的时候,生成动态的update语句,如果这个字段的值是null就不会被加入到update语句中,默认false
6)<class>元素 dynamic-insert属性:设置为true,表示把所有的<property>元素的dynamic-insert属性设置为true,默认false
7)<class>元素 dynamic-update属性:设置为true,表示把所有的<property>元素的dynamic-update属性设置为true,默认false
<class name="com.rbh,examples.Testbook" table="TESTBOOK"
dynamic-insert="true" dynamic-update="true">
</class>
</hibernate-mapping>
延迟加载(Lazy Loading)
Hibernate为了避免在关联查询中所带来的无谓的性能开销,使用了延迟加载技术,就是在真正需要读取数据的时候,才向数据库执行数据库的读取加载操作.
1.持久化对象的延迟加载(默认都为true)
<hibernate-mapping> <class name="com.rbh.examples.Category" table="CATEGORY" lazy="true"> </class> </hibernate-mapping>
2.集合对象的延迟加载(默认都为true)
<class name="com.rbh.examples.Category" table="CATEGORY" lazy="true"> <set name="products" cascade="save-update" inverse="true" lazy="true"> <key column="category_id" /> <one-to-many class="com.rbh.examples.Product"/> </set> </class>
3.属性的延迟加载(默认为false)还要对持久化的类做曾强处理
<class name="com.rbh.examples.Category" table="CATEGORY" lazy="true">
<property name="image" type=java.sql.Clob" column="IMAGE" lazy="true">
</class>
解决org.hibernate.LazyInitializationException
1.取消延迟加载(忽略)
2.使用Hibernate.initialize()
3.使用Open Session In view 设计模式
集合对象的抓取策略(Fetching strategies)
查询抓取
<set name="products" cascade="save-update" inverse="true" fetch="select"> <key column="category_id" /> <one-to-many class="com.rbh.examples.Product"/> </set>
// fetch=select ,lazy=true Session session = HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession(); Transaction tx = session.beginTransaction(); Category category = (Category) session.load(Category.class, new Integer(1)); System.out.println(category.getName()); Iterator<Product> it = category.getProducts().iterator(); Product p = null; while (it.hasNext()) { p = it.next(); System.out.println(p.getName()); } tx.commit();
使用HQL语句或者Criteria对象重载抓取策略
// fetch=select ,lazy=true,HQL重载 Session session = HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession(); Transaction tx = session.beginTransaction(); Query query = session .createQuery("select c from Category c inner join fetch c.products where c.id=? "); query.setInteger(0, new Integer(1)); Category category = (Category) query.uniqueResult(); System.out.println(category.getName()); Iterator<Product> it = category.getProducts().iterator(); Product p = null; while (it.hasNext()) { p = it.next(); System.out.println(p.getName()); } tx.commit();
子查询抓取
<set name="products" cascade="save-update" inverse="true" fetch="subselect"> <key column="category_id" /> <one-to-many class="com.rbh.examples.Product"/> </set>
// fetch=subselect ,lazy=true Session session = HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession(); Transaction tx = session.beginTransaction(); List categorys = session.createQuery("from Category").list(); for (Iterator<Category> it = categorys.iterator(); it.hasNext();) { Category category = it.next(); System.out.println(category.getName()); for (Iterator<Product> it2 = category.getProducts().iterator(); it2.hasNext();) { Product product = it2.next(); System.out.println(product.getName()); } } tx.commit();
连接查询抓取
<set name="products" cascade="save-update" inverse="true" fetch="join"> <key column="category_id" /> <one-to-many class="com.rbh.examples.Product"/> </set>
// fetch=join ,lazy=true Session session = HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession(); Transaction tx = session.beginTransaction(); Category category = (Category) session.load(Category.class, new Integer(1)); System.out.println(category.getName()); Iterator<Product> it = category.getProducts().iterator(); Product p = null; while (it.hasNext()) { p = it.next(); System.out.println(p.getName()); } tx.commit();
批量抓取
<set name="products" cascade="save-update" inverse="true" batch-size="5"> <key column="category_id" /> <one-to-many class="com.rbh.examples.Product"/> </set>
// fetch=batch ,lazy=true Session session = HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession(); Transaction tx = session.beginTransaction(); List<Category> categorys = session.createQuery("from Category").list(); System.out.println(categorys.get(3).getProducts()); System.out.println(categorys.get(0).getProducts()); tx.commit();
发表评论
-
Hibernate与Struts2,Spring
2010-04-25 19:23 1513Struts2 框架 Struts是Apache ... -
Hibernate search
2010-04-22 15:28 6169搜索引擎 全文搜索引 ... -
Hibernate Annotations
2010-04-21 14:54 6967在过去几年里,Hibernate不断发展,几乎成为Jav ... -
Hibernate的缓存
2010-04-19 19:47 1988持久层缓存的级别 事务级别的缓存 应用(进程)级别的缓存 ... -
Hibernate数据拦截与事件监听
2010-04-17 19:33 6465拦截器(Interceptor)org.hibernate.I ... -
Hibernate过滤器
2010-04-16 17:31 1456Hibernate过滤器的概念 Hibernate过滤器类 ... -
Hibernate继承映射
2010-04-15 19:48 985继承映射 3种方式,继承关系映射到数据库的表中 1.继承关 ... -
Hibernate的事务管理
2010-04-14 20:00 1281数据库事务数据库的事务指的是把一系列的数据库操作组成一个单元, ... -
Native SQL查询
2010-04-13 23:15 1184HQL写的有点累有些东西 ... -
HQL查询
2010-04-13 20:46 1491Query接口Query类型与org.hibernate.Cr ... -
Criteria查询
2010-04-12 18:42 12328Hibernate的三种查询方式 1.HQL (Hibern ... -
映射多对多关联关系
2010-04-10 15:19 1324多对多单向关联 使用多对多关联,需要借助一个起中介作用的连接 ... -
映射一对一关联关系
2010-04-09 21:08 940共享主键关系 两个关联表使用相同的主键值 类: ... -
Hibernate映射多对一关联关系
2010-04-08 15:03 1418多对一的单向关联 表于表的关联可以分为一对一,一对多,多对一 ... -
Hibernate组件映射
2010-04-07 16:03 5189组件Components 除了粗粒度的对象模型设计(一个表 ... -
Hibernate的集合映射
2010-04-06 15:31 1201Set 接口 set类型的对象,被加入的对象不能重复, ... -
浅谈OSIV与泛型DAO模式
2010-04-05 22:13 3556open session in view 简称 O ... -
java.lang.NoClassDefFoundError: org/hibernate/Session的异常
2010-03-22 15:15 7054今天写程序突然报这个错误,挺郁闷的,因为这个包是导入别人的(事 ... -
Hibernate持久化对象生命周期
2010-03-19 14:41 2300持久化对象生命周期 Persisten ... -
Hibernate标示符属性的生成策略
2010-03-18 16:53 1475标示符属性 Hibernate中的标示符属性,也可以称为Hi ...
相关推荐
珍藏的hibernate性能优化,如果对hibernate进行优化,很详细,是工作和面试的好助手
Hibernate性能优化研究.pdf..............
Hibernate性能优化策略.详细描述了java性能优化的各种策略高清
hibernate性能优化.dochibernate性能优化.doc
设计好的hibernate与设计不好的hibernate性能差别巨大,如何改善hibernate的性能是合理使用hibernate的关键
Hibernate性能优化共9页.pdf.zip
hibernate性能优化[参考].pdf
NULL 博文链接:https://quicker.iteye.com/blog/660498
首先是找到了hibernate的启动优化 http://www.hibernate.org/194.html 里面的主要思想是通过将xml序列花到本地的文件里,每次读取的时候根据情况,从本地文件读取并反序列化,节省了hibernate xml的解析时间。
初用HIBERNATE的人也许都遇到过性能问题,实现同一功能,用HIBERNATE与用JDBC性能相差十几倍很正常,如果不及早调整,很可能影响整个项目的进度。
NULL 博文链接:https://quicker.iteye.com/blog/660523
hibernate_性能优化
Hibernate3性能优化Hibernate3性能优化Hibernate3性能优化Hibernate3性能优化