我正在尝试将 2 个类关联如下
描述的代码示例如下 Bill Class
public class Bill {
@Id
@GeneratedValue(strategy = GenerationType.AUTO )
private long id;
private long billNumber;
private BillType billType;
@OneToOne
private Customer billCustomer;
//getter and setter omitted
}
客户类别的定义是
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String customerRef;
@OneToMany(fetch = FetchType.EAGER)
private List<Bill> customerBills;}
当我尝试使用标准 API 检索对象时,将检索关联的对象。
Bill bill = (Bill) session.createCriteria(Bill.class)
.add(Restrictions.eq("billNumber", BILL_NUMBER)).uniqueResult();
当我验证与客户关联的账单大小时,它被检索为空。(但 1 张账单与客户相关联)
Assert.assertEquals(1,bill.getBillCustomer().getCustomerBills().size());
(上述条件失败),但是当我用其他方式验证时,它成功了
List<Bill> billList = session.createCriteria(Customer.class)
.add(Restrictions.eq("customerRef",CUSTOMER_REF)).list();
Assert.assertEquals(1,billList.size());
我急切地加载了这些对象。我无法弄清楚我错过了什么?