-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 posts ] 
Author Message
 Post subject: Need .02 on modeling objects with BLOB attributes
PostPosted: Fri Jun 10, 2005 2:13 am 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
We're storing files in the database. Mapping files work fine and we can store and retrieve File Objects. However, if a User Object has a collection of files (File Objects) they own, providing them with a list of files causes the collection to be loaded with all of the data from the [image] column instantiated. Obviously this is unacceptable.

Any ideas on how to get around this? Perhaps a 1-1 relationship with a FileObject mapped with outer-join="false"? Not sure I like that, but I'd like to be able to load a collection of File Objects and only load the BLOB (content) when the user asks for it.

TIA,

-devon


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 10, 2005 1:00 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Just using lazy="true" for FileObjects, and lazy="true" for User.Files isn't enough?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 11, 2005 1:46 am 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
thanks sergey. that will work. i just though ti might appear to be a hack that i was creating a new object (FileObject) to get around the problem of loading the many MB of data.

-devon


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 15, 2005 4:44 am 
Regular
Regular

Joined: Fri Feb 18, 2005 3:34 am
Posts: 88
Location: Poland/Wrocław
devonl wrote:
thanks sergey. that will work. i just though ti might appear to be a hack that i was creating a new object (FileObject) to get around the problem of loading the many MB of data.


This all looks fine, but... How do you realize loading (even if a collection is lazy) all files that belong to one user. This still means loading of tons of megabytes...

I am asking 'cause have same problem... And I did not find satisfying solution yet :-(

_________________
Please rate this post if you've found it helpfull
Roland


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 15, 2005 11:57 am 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
The model makes a distinction between File Object and FileContent Object. The Member has a collection of File Objects that is essentially a collection meta data objects. Each File Object has a lazily-loaded FileContent Object that I'll only load a FileContent Object when it is requested specifically by a URL.

Hope that makes sense.

-devon


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 31, 2005 8:09 pm 
Senior
Senior

Joined: Sat Sep 10, 2005 3:46 pm
Posts: 178
This design seems decent when a file is navigated to for reading purposes, but what about when a file is overwritten. If you need to overwite the binary data, as soon as you access the file content object at the end of the one to one lazy load, the binary data will be pulled from the database and loaded into the object, but all you really wanted to do was overwite the existing data. Am I wrong on this? It seems like NHibernate should handle blobs differently since they are so different in nature.

I would like to see blobs as a proxy that were only retrieved when the get on the property was called.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 01, 2005 6:31 am 
Senior
Senior

Joined: Thu Jun 02, 2005 5:03 pm
Posts: 135
Location: Paris
Hmmm...that's actually a pretty interesting idea. Does anyone know if this is possible given the current architecture?

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 10, 2005 1:53 pm 
Senior
Senior

Joined: Sat Sep 10, 2005 3:46 pm
Posts: 178
I can not get the lazy loading of my one to one file object to work. The file objects are loaded when I try to get the collection.

Here is my mapping:

Code:
<class name="Document" table="Document" lazy="true">      
      <!--ID-->
      <id name="Id" column="DocumentId" unsaved-value="00000000-0000-0000-0000-000000000000" access="nosetter.camelcase-underscore">
         <generator class="guid" />
      </id>
            
      <!--PROPERTIES-->
      <property name="Name" column="Name" length="50" not-null="true" access="nosetter.camelcase-underscore" />
   
      <!--ASSOSCIATIONS-->
      <one-to-one name="DataFile" class="DataFile" cascade="all" access="field.camelcase-underscore"  outer-join="false" />
         
   </class>


Code:
<class name="DataFile" table="DataFile" lazy="true">      
      <!--ID-->
      <id name="Id" column="DocumentId" access="field.camelcase-underscore">
         <generator class="foreign">
            <param name="property">Document</param>
         </generator>
      </id>
            
      <!--PROPERTIES-->
      <property name="Contents" column="FileContent" type="BinaryBlob" />
   
      <!--ASSOSCIATIONS-->
      <one-to-one name="Document" class="Document" access="field.camelcase-underscore" constrained="true" />
         
   </class>


Is ther something wrong with my mapping or is lazy loading of the one-to-one just not working correctly?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 10, 2005 4:47 pm 
Senior
Senior

Joined: Thu Jun 02, 2005 5:03 pm
Posts: 135
Location: Paris
Nope, nothing wrong - this is the expected behavior. Loading a one-to-one object always loads it's connected object.

The best way around this is actually to use a one-to-one mapping on one end and a many-to-one mapping with unique="true". The many-to-one end will be the "parent" and the one-to-one end will be the "child".

The child object's id needs to be set using the "foreign" generator.

Code:
   <class name="Parent" table="Parents" lazy="true">
      
      <id name="Id" column="ParentID" unsaved-value="0" >
         <generator class="native" />
      </id>
         
      <many-to-one name="MyChild" class="Child" column="ChildID" unique="true"/>
            
   </class>


Code:
   <class name="Child" table="Children" lazy="true">
      
      <id name="Id" column="ChildID" >
         <generator class="foreign">
            <param name="property">MyParent</param>
         </generator>
      </id>
            
      <one-to-one name="MyParent" class="Parent" />

   </class>


Note that this this also allows you to have a 1 -> 0 or 1 relationship.

Hope this helps.

Cheers,

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 10, 2005 5:43 pm 
Senior
Senior

Joined: Sat Sep 10, 2005 3:46 pm
Posts: 178
Thanks Symon. This should work, the only thing that is lame is that I have to have the child Id column in my parent table as you have laid out, but I guess there is no other way.

I guess to get this really working well, we will have to wait for lazy loaded properties.

Thanks again.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 11, 2005 8:19 am 
Senior
Senior

Joined: Thu Jun 02, 2005 5:03 pm
Posts: 135
Location: Paris
Actually, my mistake - I was typing code late at night and wasn't quite as thorough as I should have been... :oops:

What I should have given for the Parent mapping is:


Code:
<class name="Parent" table="Parents" lazy="true">
     
      <id name="Id" column="ParentID" unsaved-value="0" >
         <generator class="native" />
      </id>
         
      <many-to-one name="MyChild" class="Child" column="ParentID" unique="true"/>
           
   </class>


Note that the only difference is that the many-to-one in the parent uses it's own ParentID column as the column for the child. I actually got the information on how to do this from a posting in another thread from the Hibernate forums (http://forum.hibernate.org/viewtopic.php?t=935557&highlight=lazy+load+onetoone) which covered a whole lot of other stuff. This is thanks to Regis Pires Magalhaes who made a posting at the bottom of the above thread.

Good luck!

Cheers,

Symon.

P.S. There's some explanation of why lazy loading isn't supported for one-to-one here: http://www.hibernate.org/162.html. Regis replied to this wiki entry too.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 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.