这里和hibernate中的对象导航查询特性是一样的,参照下例:
//对象导航查询
@Test
public void testFind() {
EntityManager em = MyJPAUtils.getEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
Customer customer = em.find(Customer.class, Integer.parseInt("3"));
System.out.println(customer);
Set<Contact> contacts = customer.getContacts();
System.out.println(contacts);
tx.commit();
em.close();
}
这是结果:
Hibernate:
select
customer0_.id as id1_1_0_,
customer0_.address as address2_1_0_,
customer0_.level as level3_1_0_,
customer0_.name as name4_1_0_
from
customer customer0_
where
customer0_.id=?
Customer [id=3, name=新客户, address=null, level=null]
Hibernate:
select
contacts0_.customer_id as customer5_0_0_,
contacts0_.id as id1_0_0_,
contacts0_.id as id1_0_1_,
contacts0_.address as address2_0_1_,
contacts0_.customer_id as customer5_0_1_,
contacts0_.name as name3_0_1_,
contacts0_.phone as phone4_0_1_
from
contact contacts0_
where
contacts0_.customer_id=?
[Contact [id=1, name=新联系人, address=null, phone=null]]
这里我并没有配置,因为jpa中默认一对多的导航查询是lazy的,而多对一的是eager(立即)。且在jpa中此属性名称为fetch。