hibernate fetch使用

fetch 和 lazy 配置用于数据的查询
lazy 参数值常见有 false 和 true,Hibernate3 映射文件中默认lazy = true ;
fetch 指定了关联对象抓取的方式,参数值常见是select和join,默认是select, select方式先查询主对象,再根据关联外键,每一个对象发一个select查询,获取关联的对象,形成了n+1次查询;而join方式,是left outer join查询,主对象和关联对象用一句外键关联的sql同时查询出来,不会形成多次查询。
在映射文件中,不同的组合会使用不同的查询:
1、lazy=true fetch = select ,使用延迟策略,开始只查询出主对象,关联对象不会查询,只有当用到的时候才会发出去查询 ;
2、lazy=false fetch = select ,没有用延迟策略,同时查询出主对象和关联对象,产生1+n条sql.
3、lazy=true或lazy=false fetch = join,延迟都不会作用,因为采用的是外连接查询,同时把主对象和关联对象都查询出来了.
配置还是缺乏灵活性的,开发中常会有不同的需求,所以在配置中选择默认值,在具体业务中来指定策略。再者对数据库的操作都是使用SQL来执行的,掌握了sql执行性能反过来看这些配置也许很容易了。
<?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 package="com.itrus.pojo">
	<class name="ItrusCert" table="itrus_cert">
		<id name="id" type="long">
			<column name="ID" />
			<generator class="native" />
		</id>
		<property name="certType" column="CERT_TYPE" type="string"/>
		<property name="serialNumber" column="SERIAL_NUMBER" type="string"/>
		<property name="certStatus" column="CERT_STATUS" type="string"/>
		<property name="issuerDN" column="ISSUSER_DN" type="string"/>
		<property name="subjectDN" column="SUBJECT_DN" type="string"/>
		<property name="notBefore" column="NOT_BEFORE" type="java.util.Date"/>
		<property name="notAfter" column="NOT_AFTER" type="java.util.Date"/>
		<property name="issueTime" column="ISSUE_TIME" type="java.util.Date"/>
		<property name="operateTime" column="OPERATE_TIME" type="java.util.Date"/>
		<property name="keySerialNumber" column="KEY_SERIAL_NUMBER" type="string"/>
		<property name="raId" column="RA_MARK_ID" type="long"/>
		<many-to-one name="itrusUser" column="USER_ID" class="ItrusUser"  fetch="join"/>
		<many-to-one name="itrusKey" column="KEY_ID" class="ItrusKey" fetch="join" />
		<property name="organization" column="ORGANIZATION" type="string"/>
		<property name="organizationalUnit" column="ORGANIZATION_UNIT" type="string"/>
		<property name="requestType" column="REQUEST_TYPE" type="string"/>
		<property name="commonName" column="COMMON_NAME" type="string"/>
		<property name="renewId" column="RENEW_ID" type="long"/>
		<property name="refno" column="REFNO" type="string"/>
		<property name="authcode" column="AUTHCODE" type="string"/>
		<property name="field1" column="FIELD1" type="string"/>
		<property name="field2" column="FIELD2" type="string"/>
		<property name="field3" column="FIELD3" type="string"/>
		<property name="field4" column="FIELD4" type="string"/>
		<property name="field5" column="FIELD5" type="string"/>
		<property name="orgId" column="ORG_ID" type="long"/>
		<property name="keyLength" column="KEY_LENGTH" type="string"/>
		<property name="encoding" column="ENCODING" type="string" length="4000"/>
		<property name="pkcs10" column="PKCS10" type="string" length="4000"/>
		<property name="certBuf" column="CERT_BUF" type="text" length="8000"/>
	</class>
</hibernate-mapping>


你可能感兴趣的:(fetch)