开始一直没有搞懂,
Query query = session.createQuery("from Customer c inner join c.orders");
List<Object[]> list = query.list();
这个集合里面的数组到底是怎么组成的。
于是
for(Object[] object : list){
for(int i=0;i<object.length;i++){
if(object[i] instanceof Customer){
Customer customer = (Customer)object[i];
count++;
System.out.println(customer);
System.out.println(count);
}
if(object[i] instanceof Order){
Order order = (Order)object[i];
count++;
System.out.println(order);
System.out.println(count);
}
}
}
输出:
Customer [cid=2, cname=张珂]
1
Order [oid=11, addr=成都9号, customer=Customer [cid=2, cname=张珂]]
2
Customer [cid=2, cname=张珂]
3
Order [oid=12, addr=成都4号, customer=Customer [cid=2, cname=张珂]]
4
Customer [cid=2, cname=张珂]
5
Order [oid=13, addr=成都2号, customer=Customer [cid=2, cname=张珂]]
6
Customer [cid=2, cname=张珂]
7
Order [oid=14, addr=成都0号, customer=Customer [cid=2, cname=张珂]]
8
Customer [cid=2, cname=张珂]
9
Order [oid=15, addr=成都1号, customer=Customer [cid=2, cname=张珂]]
10
Customer [cid=2, cname=张珂]
11
Order [oid=16, addr=成都6号, customer=Customer [cid=2, cname=张珂]]
12
Customer [cid=2, cname=张珂]
13
.......
由输出的结果可知,返回的集合数组中,Object[]中,是装的两个不同类的对象,一个是Customer对象,一个是与Customer对应的Order类对象。 Object[]是这两个类的父类,可以装下两个不同类的对象?(我是这样理解的)。