Hi everyone,
I am having a problem lazy-loading a Blob. This is a simplified (yet accurate) scenario of what I have:
Code:
class Parent {
@OneToMany(fetch = LAZY, mappedBy="parent")
private Set<Child> children;
/// other properties, accessors and modifiers
}
class Child {
@ManyToOne
private Parent parent;
@Basic(fetch = FetchType.LAZY)
@Column(name = "my_blob", nullable = true, length = 2147483647)
@Lob
private java.sql.Blob myBlobAttribute; // protected accessor and modifier
private long recordCount;
// other properties, accessors and modifiers
}
Have a query (criteria) that retrieves all instances of Parent. In my view, I list these instances; and with each instance the sum of children.recordCount. This is when things go haywire.
When I call children.size() from within Parent (a @Transient getter), the LAZY Blob setter is called and receives a fully initialized SerializableBlob. The problem is that some Parents may have over a million children, totalling Gigabytes of binary data that must not be loaded at once. Of course, I got an OutOfMemoryError.
One quick fix that may come in handy is to set the Blob apart in another table and make this new entity a @OneToOne(fetch = LAZY). Although this adds indirection, I could make it all protected thus keeping my public interface intact.
However, the question remains: am I doing anything wrong or is this in fact a bug?
My environment is:
Hibernate:
Core 3.2.6 GA
EntityManager 3.3.2 GA
Annotations 3.3.1 GA
DB: MySQL 5
Web application with single transaction from request receival until response rendering, which is rolled back because it is read-only in this specific case.
Help is very appreciated. Best regards,