`
fehly
  • 浏览: 247523 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Hibernate性能优化

阅读更多

使用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性能优化研究.pdf

    ### Hibernate性能优化研究 #### 一、引言 随着企业级应用的发展,高效的数据持久化技术成为了提升系统性能的关键因素之一。Hibernate作为一种流行的面向Java环境的对象关系映射(Object-Relational Mapping,简称...

    hibernate性能优化

    珍藏的hibernate性能优化,如果对hibernate进行优化,很详细,是工作和面试的好助手

    hibernate性能优化方案

    ### Hibernate性能优化方案详解 #### 一、引言 Hibernate作为Java领域中广泛使用的对象关系映射(ORM)框架,其高效性和灵活性受到众多开发者的青睐。然而,不当的设计和配置往往会导致性能瓶颈,严重影响应用程序...

    Hibernate性能优化共9页.pdf.zip

    "Hibernate性能优化共9页.pdf.zip" 这个文件标题表明了内容专注于Hibernate框架的性能优化,暗示我们将探讨如何提升使用Hibernate进行数据库操作时的效率。通常,性能优化涉及减少延迟、提高吞吐量、降低资源消耗等...

    hibernate性能优化.doc

    Hibernate 性能优化 在 Hibernate 中,性能优化是非常重要的,因为它直接影响着应用程序的效率和可扩展性。在本文中,我们将讨论两个常见的性能优化问题:批量处理和 1+n 问题,并提供相应的解决方法。 问题 1:...

    Hibernate性能优化:一级缓存

    本文将深入探讨Hibernate性能优化中的一个重要概念——一级缓存,并结合给出的压缩包文件“hibernate_cache_level1”,来详细解析一级缓存的工作原理及其优化策略。 一级缓存是Hibernate内置的一种缓存机制,它存在...

    hibernate性能优化[参考].pdf

    以下是对《hibernate性能优化[参考].pdf》内容的详细解读: 1. **数据库优化**: - **物理硬件优化**:关注磁盘的IO性能,因为数据库读写频繁,磁盘的寻道能力直接影响数据访问速度。 - **MySQL配置优化**:通过...

    Hibernate 性能优化

    ### Hibernate 性能优化 #### 一、引言 Hibernate 是一款非常强大的对象关系映射(ORM)框架,它能够简化 Java 应用程序与数据库之间的交互过程。然而,对于初次接触 Hibernate 的开发者来说,可能会遇到性能方面...

    Hibernate性能优化:二级缓存

    二级缓存是Hibernate性能优化的重要手段,通过合理配置和使用,可以显著减少数据库访问,提高系统响应速度。但同时,需要注意缓存的副作用,如数据一致性、并发控制等问题。在实际应用中,需要结合业务场景和性能...

    Struts Spring Hibernate性能优化

    在进行大型项目开发时,性能优化是至关重要的,特别是对于基于SSH(Struts、Spring、Hibernate)这样的企业级框架的应用。SSH性能优化主要是针对Struts的MVC处理、Spring的依赖注入以及Hibernate的对象关系映射进行...

    Hibernate性能调优

    ### Hibernate性能调优知识...综上所述,Hibernate性能优化涉及多个层面,包括但不限于关联管理、集合类型选择、继承关系配置以及缓存策略等。合理配置这些选项能够显著提高应用程序的性能,并且减少不必要的资源消耗。

    hibernate-性能优化

    【标签】:hibernate, 性能优化 【正文】: 1. **数据库设计调整**: - **降低关联的复杂性**:减少多对多关联,避免过度嵌套的对象关系。 - **避免联合主键**:联合主键可能导致额外的性能损失,尝试使用单独的...

    Hibernate3性能优化 Hibernate_regerence3.12

    的效率低于直接JDBC存取,然而,在经过比较好的性能优化之后,Hibernate的性能还是让人相当满意的, 特别是应用二级缓存之后,甚至可以获得比较不使用缓存的JDBC更好的性能,下面介绍一些通常的 Hibernate的优化策略...

Global site tag (gtag.js) - Google Analytics