javaEE Hibernate, 批量抓取多个对象的关联对象, batch-size

一对多关系(关联):一个Customer(客户)对应多个LinkMan(联系人)

 

Test.java:

package cn.xxx.demo;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import cn.xxx.domain.Customer;
import cn.xxx.domain.LinkMan;
import cn.xxx.utils.HibernateUtils;

//批量抓取 batch-size
public class Test {
	
	@Test
	public void fun1(){
		Session session = HibernateUtils.openSession();
		Transaction tx = session.beginTransaction();
		//----------------------------------------------------
		
		String hql = "from Customer ";
		Query query = session.createQuery(hql);
		List<Customer> list = query.list();
		
		for(Customer c:list){
			System.out.println(c.getLinkMens()); // batch-size="3" 批量抓取多个Customer的关联对象。 每次sql查询,批量查询3个Customer的所有联系人。 
		}
		
		//----------------------------------------------------
		tx.commit();
		session.close();		
	}
}

Customer.hbm.xml(配置文件,配置batch-size批量抓取):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.xxx.domain" >
	<class name="Customer" table="cst_customer" >
		<id name="cust_id"  >
			<generator class="native"></generator>
		</id>
		<property name="cust_name" column="cust_name" ></property>
		<property name="cust_source" column="cust_source" ></property>
		<property name="cust_industry" column="cust_industry" ></property>
		<property name="cust_level" column="cust_level" ></property>
		<property name="cust_linkman" column="cust_linkman" ></property>
		<property name="cust_phone" column="cust_phone" ></property>
		<property name="cust_mobile" column="cust_mobile" ></property>
	
	 <!-- 
	 	一对多关系;一个Customer对象对多个LinkMan对象。(Customer和LinkMan互为关联对象)
	 	batch-size:
	 		批量抓取多个对象的关联对象。 每次sql查询,批量查询3个Customer的所有联系人。 
	  -->
		<set name="linkMens" batch-size="3"  >
			<key column="lkm_cust_id" ></key>
			<one-to-many class="LinkMan" />
		</set>		
	
	</class>
</hibernate-mapping>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值