HIbernate注释-条件查询-加载惰性属性失败

HIbernate注释-条件查询-加载惰性属性失败,hibernate,criteria-api,Hibernate,Criteria Api,我试图将以下两个类关联起来 描述的代码示例如下所示 比尔班 public class Bill { @Id @GeneratedValue(strategy = GenerationType.AUTO ) private long id; private long billNumber; private BillType billType; @OneToOne private Customer billCustomer; //getter

我试图将以下两个类关联起来

描述的代码示例如下所示 比尔班

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;}
当我验证与客户关联的账单大小时,它被检索为null。(但有1张账单与客户关联)

(上述条件失败),但当我用其他方法验证时,它成功了

List<Bill> billList = session.createCriteria(Customer.class)
                .add(Restrictions.eq("customerRef",CUSTOMER_REF)).list();
        Assert.assertEquals(1,billList.size());
List billList=session.createCriteria(Customer.class)
.add(Restrictions.eq(“customerRef”,CUSTOMER_REF)).list();
Assert.assertEquals(1,billList.size());

我迫不及待地把东西装上。我想不出我遗漏了什么?

您的映射错误。如果关联是一对多双向关联,则一端必须将其定义为
OneToMany
,另一端必须定义为
ManyToOne
(而不是
onetomone

此外,双向关联总是有一个所有者侧和一个反向侧。相反的一侧是具有
mappedBy
属性的一侧。如果是单面体,则反方向必须是单面体。因此,映射应该是:

@ManyToOne
private Customer billCustomer;

...

@OneToMany(fetch = FetchType.EAGER, mappedBy = "billCustomer")
private List<Bill> customerBills;
@ManyToOne
私人客户;
...
@OneToMany(fetch=FetchType.EAGER,mappedBy=“billCustomer”)
私人名单客户付费;
中描述了此映射

List<Bill> billList = session.createCriteria(Customer.class)
                .add(Restrictions.eq("customerRef",CUSTOMER_REF)).list();
        Assert.assertEquals(1,billList.size());
@ManyToOne
private Customer billCustomer;

...

@OneToMany(fetch = FetchType.EAGER, mappedBy = "billCustomer")
private List<Bill> customerBills;