I'm trying to associate 2 classes as below
the code sample for description is as below 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
}
and definition of customer class is
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String customerRef;
@OneToMany(fetch = FetchType.EAGER)
private List<Bill> customerBills;}
when i try to retrive the object with criteria API, the associated objects are retrived.
Bill bill = (Bill) session.createCriteria(Bill.class)
.add(Restrictions.eq("billNumber", BILL_NUMBER)).uniqueResult();
when i validate the size of the bills associated with an customer, it retrived as null. (but 1 bill is associated to customer)
Assert.assertEquals(1,bill.getBillCustomer().getCustomerBills().size());
(the above condition fails), but when i validate with other way around, it succeeds
List<Bill> billList = session.createCriteria(Customer.class)
.add(Restrictions.eq("customerRef",CUSTOMER_REF)).list();
Assert.assertEquals(1,billList.size());
I eagerly loaded the objects. i can't figure out what i'm missing?