Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Lazy @Lob (BLOB) not so lazy
PostPosted: Fri Aug 22, 2008 8:58 am 
Regular
Regular

Joined: Wed Jan 11, 2006 12:49 pm
Posts: 64
Location: Campinas, Brazil
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,

_________________
Henrique Sousa
Don't forget to rate useful responses


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 10, 2008 6:10 am 
Newbie

Joined: Sun Nov 25, 2007 3:30 pm
Posts: 3
Few days ago I've faced similar problem - even if I've used Blob as property type and annotated it with @Lob annotation - it was not loaded lazy...

After I've done some investigation against different DBMSs and drivers for them, I got to conclusion that only driver for PostgreSQL implements Blob as realy lazy - other DBMS's (I've tried with MSSQL 2K/2K5, Oracle 11g, HSQLDB) Blob implementations are not lazy at all (please correct me if I'm wrong or you have different results)...

So, from my point of view, until vendors do not correctly implement java.sql.Blob (except PostgreSQL) it would the best to hold Blob in another table and use lazy one-to-one association...


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.