hibernate fetch使用
2015-10-09 11:42
316 查看
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>
相关文章推荐
- jQuery+PHP实现可编辑表格字段内容并实时保存
- html 美化滚动条默认样式
- 剑指offer-包含min函数的栈
- HTML中Js脚本的执行顺序
- SharePoint 2016 IT Preview的新feature列表
- chrome浏览器当表单自动填充时,怎么去除浏览器自动添加的默认样式。
- jsp <%%><%!%><%=%>
- MVC中基于Ajax和HTML5实现文件上传功能
- javascript中字符串转json
- Java之Reference使用讲解
- jQuery往返城市和日期查询实例讲解
- HtmlParser初探--使用Nodefilter和Visitor进行网页分析
- 扩展 HTMLParser 对自定义标签的处理能力
- 从HTML中攫取你所需的信息
- zeptojs中获取select选中的option的值
- htmlparser使用指南
- HTMLParser使用详解(4)- 通过Visitor访问内容
- HTMLParser使用详解(3)- 通过Filter访问内容
- css js 的引入方式和书写位置
- CSS中zoom:1的作用 ,小标签大作用