Object navigation query: When querying an object, query all related objects through this object.
Based on many operating table article, create a test class:
One check and many (check all contacts associated with the customer according to the customer)
1. getOne(Long id) defaults to lazy loading
@Test
@Transactional //解决在java代码中的no session问题
public void testQuery1() {
//查询id为 1 的客户
Customer customer = customerDao.getOne(1L);
//对象导航查询,此客户下的所有联系人
Set<LinkMan> linkMans = customer.getLinkMans();
for (LinkMan linkMan : linkMans) {
System.out.println(linkMan);
}
}
2. findOne(Long id) is loaded immediately by default
Configure one-to-many table relationship mapping in one class to load immediately (fetch = FetchType.EAGER)
/**
* fetch : 配置关联对象的加载方式
* EAGER :立即加载
* LAZY :延迟加载
*/
@OneToMany(mappedBy = "customer", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Set<LinkMan> linkMans = new HashSet<>();
@Test
@Transactional //解决在java代码中的no session问题
public void testQuery2() {
//查询id为 1 的客户
Customer customer = customerDao.findOne(1L);
//对象导航查询,此客户下的所有联系人
Set<LinkMan> linkMans = customer.getLinkMans();
System.out.println(linkMans.size());
}
Check one more (check the customer associated with the contact according to the contact) The default is to load immediately
Lazy loading of many-to-one table relationship mapping can be configured in the multi-party class (fetch = FetchType.LAZY):
@ManyToOne(targetEntity = Customer.class, fetch = FetchType.LAZY)
@JoinColumn(name = "lkm_cust_id", referencedColumnName = "cust_id")
private Customer customer;
@Test
@Transactional // 解决在java代码中的no session问题
public void testQuery3() {
//查询id为 1 的联系人
LinkMan linkMan = linkManDao.findOne(1L);
//对象导航查询所属的客户
Customer customer = linkMan.getCustomer();
System.out.println(customer);
}
*** recommended many queries