Thanks for the advice. However, I went in another direction, and for
future reference to other people with the same problem, this is what I did:

I have three classes, Product, Image and ImageWithData.

Product contains a collection of Image, and ImageWithData inherits Image.

Both Image and ImageWithData points to the same table, where
ImageWithData also specifies a BLOB with type byte[]

When I create an image with binary data, I create new ImageWithData()
and persist it before I add it to the Image-collection of Product.

When fetching a Product, I get a collection with Image's without the
binary data. When I need the binary data, I have constructor for
ImageWithData that takes Image as argument,

new ImageWithData(Image img)

where I transport all properties to the new one (I only need the primary
key though if I refresh it) , then I run Refresh() on the object and it
fetches the binary data.

This demands some overhead (I need to persist ImageWithData manually,
aswell as manual refresh when loading the data) but gives me a little
more control of the "laziness" as oppose to when NHibernate deals with
it. I heavily depend on JSON-serialization and it seems that it triggers
the loading of my collections even though I specifically tell it to
ignore the collections.
 
If anyone can see more downsides to this approach, please let me know.

/Jimmy

PKetelle wrote:
> Ignore my last advise as I totally had a mind blank.
>
> I would recommend using this
>
> [HasAndBelongsToMany(typeof(ImageBinaryData), Table =
> "ImageDataRefTable", ColumnKey = "ImageDataID", ColumnRef =
> "ImageBinaryDataID", Lazy = true)]
>         public IList< ImageBinaryData > ImageData
>         {
>             get { return imageData; }
>             set { imageData = value; }
>         }
>
> to do this you would have three tables.
> 1) contains image data ( ImageDataID, filename, size, mime-type, etc )
> 2) cross reference table ( ImageDataID, ImageBinaryDataID )
> 3) ImageBinaryData ( ImageBinaryDataID, BinaryData )
>
> This should solve your problem.
>
> On Oct 17, 4:35 pm, Jimmy Shimizu <[EMAIL PROTECTED]> wrote:
>   
>> I'm trying to achieve scalability and to centralize managment of the
>> original images, I want to store them in the DB to keep a constraint aswell.
>>
>> It could be, however, solved using dedicated mediaservers, using
>> networked mapped drives for that matter. Space seldom is an issue
>> nowadays, although if I cannot get this lazy loading to work I'm stuck
>> anyway.
>>
>> My syntax was somehow incorrect, you cannot have Lazy on a BelongsTo, it
>> was only the class-attribute that had Lazy.
>>
>> It has the effect that it actually selects imageData in a separate
>> query, but it doesn't become proxied and therefor is always loaded when
>> lookup up a Product.
>>
>> I had another thought, about having Image and extend that with
>> ImageWithData, where Image would never be aware of the binary-data. Both
>> would map against the same table, and when instantiated as Image it
>> would never be aware of any binarydata, hence, not load it. Not sure if
>> this approach is doable though, the plan was to just typecast it to
>> ImageWithData and refresh it when I needed the binary data.
>>
>> PKetelle wrote:
>>     
>>> Would it not be easier to have the Images saved on the local disc and
>>> just keep the paths in the database to reduce the database size from
>>> going crazy ?
>>>       
>>> However would the Image Class not be
>>>       
>>> [ActiveRecord]
>>> public class Image
>>> {
>>>         [PrimaryKey( PrimaryKeyType.Native)]
>>>         public override int Id { get; set; }
>>>         private ImageData data;
>>>         [BelongsTo("ImageData", Lazy = true)]
>>>         public MyImageData Data
>>>         {
>>>             get { return data; }
>>>             set { data = value; }
>>>         }
>>> }
>>>       
>>> as this is how I have done Lazy loading before.
>>>       
>>> I do sometimes find that if you have not accessed the ImageData Value
>>> before you render the view that you can't access the ImageData at all.
>>>       
>>> On Oct 17, 10:44 am, Jimmy Shimizu <[EMAIL PROTECTED]> wrote:
>>>       
>>>> I'm trying to enable lazy-loading of a Property in one of my entities,
>>>> but I keep running into trouble.
>>>>         
>>>> Explanation of problem:
>>>>         
>>>> I have an Image-class that contains info about an image, filename, size,
>>>> type etc, including binary data of the object. Since the binary data can
>>>> become huge, I need to be able to load the image without that data
>>>> unless explicitly addressed.
>>>>         
>>>> Since you cannot specify Lazy on a property, I needed another solution
>>>> to the problem. First idea was to use a Nested class, but that however
>>>> doesn't support Lazy either.
>>>>         
>>>> Second idea was to use a OneToOne relation, but that doesn't support
>>>> Lazy either.
>>>>         
>>>> Third idea, found 
>>>> here:http://gnschenker.blogspot.com/2007/06/one-to-one-mapping-and-lazy-lo...,
>>>> when using OneToOne with a BelongsTo gives the possibility to add Lazy
>>>> to the BelongsTo, however I don't get this to work.
>>>>         
>>>> I can't seem to get my head around this Lazy-loading. This is my setup:
>>>>         
>>>> [ActiveRecord( "`product`", "products")]
>>>> public class Product
>>>> {
>>>>         [HasAndBelongsToMany( typeof( Image ), Lazy = true, Inverse = 
>>>> false]
>>>>         public virtual ISet<Image> Images {}
>>>>         
>>>> }
>>>>         
>>>> [ActiveRecord( Lazy = true )]
>>>> public class Image
>>>> {
>>>>         [PrimaryKey( PrimaryKeyType.Native)]
>>>>         public override int Id { get; set; }
>>>>         
>>>>         private ImageData data;
>>>>         
>>>>         [BelongsTo(Lazy = true)]
>>>>         public virtual ImageData Data
>>>>         {
>>>>             get { return this.data; }
>>>>             set
>>>>             {
>>>>                 value.Image = this;
>>>>                 this.data = value;
>>>>             }
>>>>         }
>>>>         
>>>> }
>>>>         
>>>> [ActiveRecord( Lazy = true )]
>>>>     public class ImageData
>>>>     {
>>>>         [PrimaryKey( PrimaryKeyType.Foreign )]
>>>>         public virtual int Id { get; set; }
>>>>         
>>>>         [OneToOne( Constrained = true )]
>>>>         public virtual Image Image { get; set; }
>>>>         
>>>> }
>>>>         
>>>> The code is heavily simplified, but this setup gives me:
>>>> NHibernate.Id.IdentifierGenerationException: null id generated for:
>>>> ImageData
>>>>         
>>>> If I try to use OneToOne on both Entities (Image and ImageData) it seem
>>>> to work, but Lazy doesn't work.
>>>>         
>>>> Another weird issue is that when having the Images-collection on Product
>>>> set to Lazy, it still doesn't generate proxy-objects of Images, I
>>>> thought that was the primary goal with Lazy? Where can I find some
>>>> useful guides regarding Lazy-loading that can explain it in detail?
>>>>         
>>>> Even Lazy on my ImageData-class doesn't seem to make any difference, I
>>>> would assume that having OneToOne on everything, then Lazy on ImageData
>>>> would generate proxy-object, but that doesn't seem to be the case.
>>>>         
>>>> // Jimmy
>>>>         
> >
>   


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Castle Project Users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/castle-project-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to