HIbernate Annotations - criteria query - loading lazy properties fail

I'm trying to associate 2 classes as below

the code sample for description is as below Bill Class

public class Bill {
    @GeneratedValue(strategy = GenerationType.AUTO )
    private long id;
    private long billNumber;
    private BillType billType;
    private Customer billCustomer;

//getter and setter omitted

and definition of customer class is

public class Customer {
    @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)


(the above condition fails), but when i validate with other way around, it succeeds

List<Bill> billList = session.createCriteria(Customer.class)

I eagerly loaded the objects. i can't figure out what i'm missing?


Your mapping is wrong. If the association is a one-to-many bidirectional association, one side must define it as OneToMany, and the other side as ManyToOne (not OneToOne).

Moreover, a bidirectional association always has an owner side and an inverse side. The inverse side is the one with the mappedBy attribute. In the case of a OneToMany, the inverse side must be the one side. So the mapping should be:

private Customer billCustomer;


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

This mapping is described in the hibernate documentation.

Need Your Help

What is the lifeCycle of a .plist file

iphone ios4

I am totally confused while using a plist file.

Which method performs better: .Any() vs .Count() > 0?

.net linq performance .net-3.5 extension-methods

in the System.Linq namespace, we can now extend our IEnumerable's to have theAny() and Count() extension methods.

What is the best way to display HTML in Flex?

html flex actionscript text

I have HTML that includes symbols such as the Trademark "TM" as superscript (&trade;). In normal HTML, I would use "&amp;trade;" or &amp;#153; to display the Trademark TM. However, I can find no wa...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.