- 浏览: 264835 次
- 性别:
- 来自: 成都
文章分类
- 全部博客 (87)
- Java (6)
- Frameworks (1)
- JavaWeb (3)
- Spring (6)
- Hibernate (26)
- Maven (2)
- Plugin (1)
- Velocity (1)
- Freemarker (1)
- Spring security (18)
- Google (5)
- Guice (5)
- rmi (1)
- Annotation (1)
- Binding (1)
- commons-fileupload (3)
- fileupload (3)
- ehcache (1)
- ApplicationContext (1)
- Resource (1)
- Validator (1)
- PropertyEditor (1)
- one-to-many (5)
- cascade (2)
- MessageSource (1)
- Serialize (1)
- Serializable (1)
- delete (1)
- delete-orphan (1)
- fetch (1)
- one-to-one (1)
- join (4)
- DomainObject (1)
- CRUD (1)
- composite-element (1)
- Hibernate ORM (14)
- dynamic-insert (1)
- dynamic-update (1)
- Search (1)
- DDD (0)
- Job (0)
- element (1)
- Unique (1)
- Group (1)
- tomcat (1)
- https (1)
- mysql (1)
最新评论
-
xurunchengof:
[url][url][url][img][img][img][ ...
Spring Security 3多用户登录实现之三 验证过滤器 -
Wind_ZhongGang:
yo8237233 写道你这样的话如果上传文件超过了50000 ...
Spring集成Commons fileupload,文件上传 -
yo8237233:
你这样的话如果上传文件超过了50000000就会报异常
Spring集成Commons fileupload,文件上传 -
zhuzhiguosnail:
Wind_ZhongGang 写道lianglaiyang 写 ...
Spring Security 3多用户登录实现一 -
曾老师:
?????
Spring Security 3用户登录实现之十 用户切换
Hibernate fetch抓取策略定义了当抓取关联对象时,以怎样的一种方式进行抓取。当抓取策略为join时,抓取关联对象是通过连接表来抓取的,只会发送一条sql语句,即可完成主对象与关联对象的抓取,而当抓取策略为select时,抓取关联对象则会发送两条sql语句来完成关联对象的抓取,一条是抓取主对象,而另一条则是抓取关联对象。抓取策略通常会与延迟加载策略合并使用,这是因为当采用延迟加载策略时,如果抓取策略的设置不适当,会造成想象之外的异常,而当采用非延迟加载策略时,即即时加载策略,通常的建议是采用join即连接抓取策略,这样避免了向数据库发送多条sql语句。需要注意的是,只有当使用get或load方法来获取对象时,抓取策略才会起作用,如果查找对象采用的是自定义的hql或sql语句,抓取策略不会起作用。其中get方法是即时加载对象,而load则是延迟加载对象,只有当使用到该对象时,才会向数据库发送查询语句,以查找出该对象。
一。Husband
package com.dream.model.join; import java.util.Set; /** * Created by IntelliJ IDEA. * User: Zhong Gang * Date: 9/26/11 * Time: 5:46 PM */ public class Husband { private Integer id; private String name; private Set<Wife> wifes; public Husband(String name) { this.name = name; } public Husband() { } public Husband(String name, Set<Wife> wifes) { this.name = name; this.wifes = wifes; } public Set<Wife> getWifes() { return wifes; } public Integer getId() { return id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping default-access="field"> <class name="com.dream.model.join.Husband" table="husband" dynamic-insert="true" dynamic-update="true"> <id name="id" column="id" type="java.lang.Integer"> <generator class="native"/> </id> <property name="name" column="name" type="java.lang.String"/> <set name="wifes" table="wife" cascade="all" lazy="false" fetch="join"> <key column="husbandid"/> <one-to-many class="com.dream.model.join.Wife"/> </set> </class> </hibernate-mapping>
二。Wife
package com.dream.model.join; /** * Created by IntelliJ IDEA. * User: Zhong Gang * Date: 9/26/11 * Time: 5:47 PM */ public class Wife { private Integer id; private String name; private Husband husband; public Wife(String name) { this.name = name; } public Wife() { } public Wife(String name, Husband husband) { this.name = name; this.husband = husband; } public String getName() { return name; } public Husband getHusband() { return husband; } }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping default-access="field"> <class name="com.dream.model.join.Wife" table="wife" dynamic-insert="true" dynamic-update="true"> <id name="id" column="id" type="java.lang.Integer"> <generator class="native"/> </id> <property name="name" column="name" type="java.lang.String"/> <many-to-one name="husband" class="com.dream.model.join.Husband" column="husbandid"/> </class> </hibernate-mapping>
三。CoupleDao
package com.dream.dao.standard; import com.dream.model.join.Husband; import com.dream.model.join.Wife; import java.util.Set; /** * Created by IntelliJ IDEA. * User: Zhong Gang * Date: 9/26/11 * Time: 5:51 PM */ public interface CoupleDao { Husband findHusbandById(Integer id); void saveOrUpdateHusband(Husband husband); }
package com.dream.dao; import com.dream.dao.standard.CoupleDao; import com.dream.model.join.Husband; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import java.util.List; /** * Created by IntelliJ IDEA. * User: Zhong Gang * Date: 9/26/11 * Time: 5:52 PM */ public class CoupleDaoImpl extends HibernateDaoSupport implements CoupleDao { public Husband findHusbandById(Integer id) { return getHibernateTemplate().get(Husband.class, id); } public void saveOrUpdateHusband(Husband husband) { getHibernateTemplate().saveOrUpdate(husband); } }
四。CoupleService
package com.dream.service.standard; import com.dream.model.join.Husband; /** * Created by IntelliJ IDEA. * User: Zhong Gang * Date: 9/26/11 * Time: 5:53 PM */ public interface CoupleService { Husband loadHusbandById(Integer id); void saveOrUpdateHusband(Husband husband); }
package com.dream.service; import com.dream.dao.standard.CoupleDao; import com.dream.exception.DataNotExistException; import com.dream.model.join.Husband; import com.dream.service.standard.CoupleService; /** * Created by IntelliJ IDEA. * User: Zhong Gang * Date: 9/26/11 * Time: 5:53 PM */ public class CoupleServiceImpl implements CoupleService { private CoupleDao coupleDao; public Husband loadHusbandById(Integer id) { Husband husband = coupleDao.findHusbandById(id); if (husband == null) { throw new DataNotExistException(); } return husband; } public void saveOrUpdateHusband(Husband husband) { coupleDao.saveOrUpdateHusband(husband); } public void setCoupleDao(CoupleDao coupleDao) { this.coupleDao = coupleDao; } }
package com.dream.exception; /** * Created by IntelliJ IDEA. * User: Zhong Gang * Date: 9/26/11 * Time: 6:01 PM */ public class DataNotExistException extends RuntimeException { public DataNotExistException() { super("The data you find does not exist in the database."); } }
五。testDatasource
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" default-autowire="byName"> <context:property-placeholder location="classpath:testDB.properties"/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${db.driver}"/> <property name="url" value="${db.url}"/> <property name="username" value="${db.username}"/> <property name="password" value="${db.password}"/> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mappingLocations"> <list> <value>/hibernate_mappings/Husband.hbm.xml</value> <value>/hibernate_mappings/Wife.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> <prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop> </props> </property> </bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="service" expression="execution(* com.dream.service..*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="service"/> </aop:config> <bean id="coupleDao" class="com.dream.dao.CoupleDaoImpl"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <bean id="coupleService" class="com.dream.service.CoupleServiceImpl"> <property name="coupleDao" ref="coupleDao"/> </bean> </beans>
六。testDB
db.url=jdbc:mysql://localhost:3306/test_fetch db.driver=com.mysql.jdbc.Driver db.username=root db.password=root hibernate.dialect=org.hibernate.dialect.MySQL5Dialect hibernate.show_sql=true hibernate.hbm2ddl.auto=update hibernate.jdbc.batch_size=100
七。TestCase
package com.fetch; import com.dream.model.join.Husband; import com.dream.model.join.Wife; import com.dream.service.standard.CoupleService; import junit.framework.TestCase; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.HashSet; import java.util.Set; /** * Created by IntelliJ IDEA. * User: Zhong Gang * Date: 9/26/11 * Time: 6:05 PM */ public class HibernateFetchTest extends TestCase { private CoupleService coupleService; @Override public void setUp() throws Exception { ApplicationContext context = new ClassPathXmlApplicationContext("classpath:testDatasource.xml"); coupleService = (CoupleService) context.getBean("coupleService"); } public void testCascadeAllDelete() throws Exception { Wife wife1 = new Wife("Wife1"); Wife wife2 = new Wife("Wife2"); Set<Wife> wifes = new HashSet<Wife>(); wifes.add(wife1); wifes.add(wife2); Husband husband = new Husband("Husband1", wifes); coupleService.saveOrUpdateHusband(husband); Husband husbandAfterFound = coupleService.loadHusbandById(husband.getId()); Set<Wife> wifeSet = husbandAfterFound.getWifes(); assertEquals(2, wifeSet.size()); } }
Scenario 1:lazy="true" fetch="join"
Hibernate: insert into husband (name) values (?) Hibernate: insert into wife (name) values (?) Hibernate: insert into wife (name) values (?) Hibernate: update wife set husbandid=? where id=? Hibernate: update wife set husbandid=? where id=? Hibernate: select husband0_.id as id0_1_, husband0_.name as name0_1_, wifes1_.husbandid as husbandid0_3_, wifes1_.id as id3_, wifes1_.id as id1_0_, wifes1_.name as name1_0_, wifes1_.husbandid as husbandid1_0_ from husband husband0_ left outer join wife wifes1_ on husband0_.id=wifes1_.husbandid where husband0_.id=?
只发送了一条sql语句便查找出了主对象及关联对象。
Scenario 2:lazy="true" fetch="select"
Hibernate: insert into husband (name) values (?) Hibernate: insert into wife (name) values (?) Hibernate: insert into wife (name) values (?) Hibernate: update wife set husbandid=? where id=? Hibernate: update wife set husbandid=? where id=? Hibernate: select husband0_.id as id0_0_, husband0_.name as name0_0_ from husband husband0_ where husband0_.id=? org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.dream.model.join.Husband.wifes, no session or session was closed at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383) at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375) at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:122) at org.hibernate.collection.PersistentSet.size(PersistentSet.java:162) at com.fetch.HibernateFetchTest.testCascadeAllDelete(HibernateFetchTest.java:42) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at com.intellij.junit3.JUnit3IdeaTestRunner.doRun(JUnit3IdeaTestRunner.java:139) at com.intellij.junit3.JUnit3IdeaTestRunner.startRunnerWithArgs(JUnit3IdeaTestRunner.java:52) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:199) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:62) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
当采用延迟加载策略的同时,使用select抓取策略时,因为只有当对象使用时才会发起sql语句,这时因为没有了session的管理,会造成异常。
Scenario 3:lazy="false" fetch="join"
Hibernate: insert into husband (name) values (?) Hibernate: insert into wife (name) values (?) Hibernate: insert into wife (name) values (?) Hibernate: update wife set husbandid=? where id=? Hibernate: update wife set husbandid=? where id=? Hibernate: select husband0_.id as id0_1_, husband0_.name as name0_1_, wifes1_.husbandid as husbandid0_3_, wifes1_.id as id3_, wifes1_.id as id1_0_, wifes1_.name as name1_0_, wifes1_.husbandid as husbandid1_0_ from husband husband0_ left outer join wife wifes1_ on husband0_.id=wifes1_.husbandid where husband0_.id=?
使用即时加载策略的同时,使用连接抓取策略,只会发送一条连接sql语句便会查找出主对象及关联对象。
Scenario 4:lazy="false" fetch="select"
Hibernate: insert into husband (name) values (?) Hibernate: insert into wife (name) values (?) Hibernate: insert into wife (name) values (?) Hibernate: update wife set husbandid=? where id=? Hibernate: update wife set husbandid=? where id=? Hibernate: select husband0_.id as id0_0_, husband0_.name as name0_0_ from husband husband0_ where husband0_.id=? Hibernate: select wifes0_.husbandid as husbandid0_1_, wifes0_.id as id1_, wifes0_.id as id1_0_, wifes0_.name as name1_0_, wifes0_.husbandid as husbandid1_0_ from wife wifes0_ where wifes0_.husbandid=?使用即时加载策略的同时,使用select抓取策略,会发送两条sql语句,第一条用于查询出主对象,而第二条则会查询出关联对象。
发表评论
-
Hibernate unique使用
2011-11-28 22:16 1177在Hibernate中对于一对一的关联关系配置 ... -
Hibernate cascade在不同关联关系中的具体表现
2011-11-22 21:47 3142在Hibernate中,针对持久化实体的配置文件中有 ... -
Hibernate element使用
2011-11-20 20:43 1590一.Person package co ... -
Hibernate 单表实现树结构
2011-11-13 21:10 2842来看看通过单表来实现一个树形结构的方式。这里假设有这样 ... -
Hibernate save persist merge探究
2011-10-27 17:59 4139先来看看hibernate-reference.pd ... -
Hibernate Search配置及简单应用
2011-10-25 16:06 2583Hibernate Search是一款集成Apach ... -
Hibernate的三种配置方式
2011-10-24 16:58 1813对于Hibernate的配置方式,有三种,一种是通过 ... -
dynamic-insert,dynamic-update 我有话要说
2011-10-21 17:39 13000在之前的一个项目中,Team leader说在每一个 ... -
Hibernate one-to-many composite-element实现
2011-10-17 16:55 2653Hibernate中持久化实体间一对多关联关系的compo ... -
基于DomainObject的通用数据库CRUD操作
2011-10-17 15:09 1615对于项目中的持久化实体,持久化实体虽然对应着不同的业务对象 ... -
Hibernate one-to-many 双方双向连接表关联
2011-10-17 14:24 1740Hibernate中持久化实体间一对多关联,具体关联关 ... -
Hibernate one-to-many 多方单向连接表关联
2011-10-15 23:37 1299Hibernate中持久化实体间一对多 ... -
Hibernate one-to-many 一方单向连接表关联
2011-10-15 23:17 1707Hibernate中对于持久化实体间的关联关系有 ... -
Hibernate one-to-one 两方双向连接表关联
2011-10-15 17:38 2756Hibernate中对于持久化实体间的关联关系有三种分类, ... -
Hibernate 关联关系解除
2011-09-27 13:46 1548Hibernate对于持久化实体间的关联关系解 ... -
Hibernate one-to-many cascade探究
2011-09-08 13:20 1159对于Hibernate的one-to-many关联关系中, ... -
Hibernate inverse 深度解析
2011-08-09 22:55 1276inverse 英文意思为反向,倒转的。 Hi ... -
Hibernate ORM one-to-one主键关联关系映射
2011-08-01 22:08 1398Hibernate ORM one-to-one映射主要有 ... -
Hibernate ORM Inheritance
2011-07-31 22:56 1374Hibernate ORM提供了三 ... -
Hibernate many-to-many
2011-07-30 17:20 1897对于持久化实体间多对多的关联关系,在数据库中的反映 ...
相关推荐
今天我们要探讨的是Hibernate的抓取策略,特别是针对set集合的策略。在大型应用中,有效地管理数据加载能显著提升性能,减少数据库交互次数,这就是抓取策略的核心作用。 首先,我们需要理解什么是抓取策略。在...
本篇主要关注的是Hibernate的抓取策略,特别是批量抓取技术,这对于提高应用程序性能至关重要。批量抓取能够减少数据库交互次数,从而优化查询效率。 首先,我们要理解什么是抓取策略。在Hibernate中,抓取策略是指...
为了解决这个问题,我们可以使用预加载(eager loading)或者子集加载(fetch plan)等抓取策略。 在"many-to-one"关系中,一个实体可以与多个其他实体相关联。默认情况下,Hibernate会设置这种关联为懒加载,即...
在Java的持久化框架Hibernate中,数据访问优化是至关重要的,而抓取策略(Fetch Strategy)和懒加载(Lazy Loading)则是实现这一目标的关键技术。本文将深入探讨这两个概念,并通过具体的案例进行分析。 首先,让...
描述:本文深入解析了HIBERNATE的检索策略,为读者提供了一条清晰的学习路径,重点分析了HIBERNATE中的抓取策略及其应用场景,帮助开发者更有效地管理和优化数据加载过程。 在HIBERNATE框架中,检索策略主要涉及...
本章主要探讨了如何通过不同的抓取策略来提升Hibernate的性能。抓取策略涉及到当应用程序需要处理对象间的关联时,Hibernate如何有效地获取关联数据。 首先,连接抓取(Join fetching)是一种通过在SELECT语句中...
- **抓取策略**:通过设置`fetch`属性,可以控制关联对象的加载方式: - `select`:默认值,使用单独的SELECT语句获取关联对象。 - `join`:使用迫切左外连接(Eager Fetching),一次性加载关联对象。 - `...
在Hibernate Day 04的学习中,我们深入探讨了如何利用Hibernate进行查询操作以及它的抓取策略,这些是理解并有效使用Hibernate的关键部分。 首先,让我们来详细了解一下Hibernate的查询方式。Hibernate提供了一个...
学习成长路,Hibernate总结: 1.Hibernate入门优缺点、 2.Hibernate的操作CRUD、 3.主键生成机制、 4.持久化对象的状态、 ...8.Hibernate检索策略(fetch抓取策略)、 9.二级缓存、 10.Hbernate的检索方式(HQL语句)
hibernate.default_batch_fetch_size 参数用于设置 Hibernate 关联的批量抓取默认数量。其取值建议为 4、8 或 16。 9. hibernate.default_entity_mode hibernate.default_entity_mode 参数用于指定由这个 ...
例如,通过设置合适的 `hibernate.max_fetch_depth` 和 `hibernate.default_batch_fetch_size` 可以显著提升应用性能;而启用 `hibernate.generate_statistics` 则有助于我们更好地监控和分析应用性能瓶颈。此外,...
### Hibernate框架基础四:深入理解HQL多表检索、事务及隔离级别、HQL优化与抓取策略 #### 一、HQL多表检索 **HQL (Hibernate Query Language)** 是一种面向对象的查询语言,它允许开发人员使用类名和属性名来执行...
13. **hibernate.jdbc.fetch_size**: 设置JDBC抓取(fetch)大小,非零值,通过`Statement.setFetchSize()`控制。 14. **hibernate.jdbc.batch_size**: 用于启用JDBC2批量更新,建议值在5到30之间。 15. **...
Hibernate.max_fetch_depth 属性用于控制 Hibernate 的外连接抓取(outer join fetch)树的最大深度。该属性的值可以是 0 到 3 之间的整数,建议的取值是 0 到 3。 hibernate.default_batch_fetch_size Hibernate....
6. **hibernate.max_fetch_depth**:控制单向关联的外连接抓取深度,较高的值可能导致更多的数据库查询,建议值在0到3之间。 7. **hibernate.default_batch_fetch_size**:默认的批量抓取大小,可以提高性能,常见...
- `hibernate.max_fetch_depth`:控制外连接抓取的深度,默认值为0表示关闭。 - `hibernate.default_batch_fetch_size`:设置默认的批量抓取大小,提高查询性能。 - `hibernate.default_entity_mode`:定义...
1. **hibernate.jdbc.fetch_size**:设置JDBC的抓取大小,以优化结果集的处理。 2. **hibernate.jdbc.batch_size**:用于批量更新操作,提高性能。通常建议值在5到30之间。 3. **hibernate.jdbc.batch_versioned_...
当业务层需要为Web层提供完整的数据集合时,确保在Session关闭前调用`Hibernate.initialize()`来初始化集合,或者在查询中使用`FETCH`从句或`FetchMode.JOIN`以预加载所需的数据。这避免了因延迟加载导致的多次...
19.1.2. 调整抓取策略(Tuning fetch strategies) 19.1.3. 单端关联代理(Single-ended association proxies) 19.1.4. 实例化集合和代理(Initializing collections and proxies) 19.1.5. 使用批量抓取(Using...