使用场景:
在映射文件中用<set>元素配置一对多关联及多对多关联,set每次查询的数量,以免频繁访问数据库。
关键代码:
映射文件的<set>节点加上 batch-size="5"
详细代码:
User.hbm.xml:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 2016-6-25 14:50:51 by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <class name="com.shuoeasy.test.User" table="USER" lazy="true"> <id name="id" type="int"> <column name="ID" /> <!-- 指定主键的生成方式,native:使用数据库本地方式 --> <generator class="native" /> </id> <property name="userName" type="java.lang.String"> <column name="USERNAME" /> </property> <!-- batch-size:设定一次初始化set集合的数据了,默认为1 --> <set name="orders" table="ORDERS" inverse="true" batch-size="5"> <key column="USER_ID"></key> <one-to-many class="com.shuoeasy.test.Orders"/> </set> </class> </hibernate-mapping>
sql的区别
不设置batch-size:
Hibernate: select user0_.ID as ID1_1_, user0_.USERNAME as USERNAME2_1_ from USER user0_ Hibernate: select orders0_.USER_ID as USER_ID3_1_0_, orders0_.ID as ID1_0_0_, orders0_.ID as ID1_0_1_, orders0_.TITLE as TITLE2_0_1_, orders0_.USER_ID as USER_ID3_0_1_ from Orders orders0_ where orders0_.USER_ID=? Hibernate: select orders0_.USER_ID as USER_ID3_1_0_, orders0_.ID as ID1_0_0_, orders0_.ID as ID1_0_1_, orders0_.TITLE as TITLE2_0_1_, orders0_.USER_ID as USER_ID3_0_1_ from Orders orders0_ where orders0_.USER_ID=? Hibernate: select orders0_.USER_ID as USER_ID3_1_0_, orders0_.ID as ID1_0_0_, orders0_.ID as ID1_0_1_, orders0_.TITLE as TITLE2_0_1_, orders0_.USER_ID as USER_ID3_0_1_ from Orders orders0_ where orders0_.USER_ID=? Hibernate: select orders0_.USER_ID as USER_ID3_1_0_, orders0_.ID as ID1_0_0_, orders0_.ID as ID1_0_1_, orders0_.TITLE as TITLE2_0_1_, orders0_.USER_ID as USER_ID3_0_1_ from Orders orders0_ where orders0_.USER_ID=? Hibernate: select orders0_.USER_ID as USER_ID3_1_0_, orders0_.ID as ID1_0_0_, orders0_.ID as ID1_0_1_, orders0_.TITLE as TITLE2_0_1_, orders0_.USER_ID as USER_ID3_0_1_ from Orders orders0_ where orders0_.USER_ID=? Hibernate: select orders0_.USER_ID as USER_ID3_1_0_, orders0_.ID as ID1_0_0_, orders0_.ID as ID1_0_1_, orders0_.TITLE as TITLE2_0_1_, orders0_.USER_ID as USER_ID3_0_1_ from Orders orders0_ where orders0_.USER_ID=? Hibernate: select orders0_.USER_ID as USER_ID3_1_0_, orders0_.ID as ID1_0_0_, orders0_.ID as ID1_0_1_, orders0_.TITLE as TITLE2_0_1_, orders0_.USER_ID as USER_ID3_0_1_ from Orders orders0_ where orders0_.USER_ID=?
设置 batch-size="5" :
Hibernate: select user0_.ID as ID1_1_, user0_.USERNAME as USERNAME2_1_ from USER user0_ Hibernate: select orders0_.USER_ID as USER_ID3_1_1_, orders0_.ID as ID1_0_1_, orders0_.ID as ID1_0_0_, orders0_.TITLE as TITLE2_0_0_, orders0_.USER_ID as USER_ID3_0_0_ from Orders orders0_ where orders0_.USER_ID in (?, ?, ?, ?, ?) Hibernate: select orders0_.USER_ID as USER_ID3_1_1_, orders0_.ID as ID1_0_1_, orders0_.ID as ID1_0_0_, orders0_.TITLE as TITLE2_0_0_, orders0_.USER_ID as USER_ID3_0_0_ from Orders orders0_ where orders0_.USER_ID in (?, ?)