Hi all,
I'm using NHiberbate 1.2.1 and I'm trying to use lazy one-to-one association.
Let me describe the situation shortly.
My application is intented to store files inside MS SQL Server. But I want to store binary data in the separate table. So I've created ArchiveFile and FileContent classes and I'm trying to use one-to-one association - to avoid loading files content if application just builds the list of files in the database. All properties in the classes are virtual.
Here is ArchiveFile.hbm.xml:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Archiving.BusinessLayer" namespace="BusinessLayer.Entities">
<class name="ArchiveFile" table="ArchiveFiles">
<id name="ID">
<generator class="identity"/>
</id>
<property name="Date" type="DateTime" />
<property name="FileName" type="String" />
<property name="FileType" column="Type" type="int" />
<many-to-one name="Archive" class="Archive" column="ArchiveID"/>
<one-to-one name="FileContent" class="FileContent" cascade="all"/>
</class>
</hibernate-mapping>
Here is the FileContent.hbm.xml:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Archiving.BusinessLayer" namespace="Archiving.BusinessLayer.Entities">
<class name="FileContent" table="FileContent">
<id name="FileID" column="FileID">
<generator class="foreign">
<param name="property">ArchiveFile</param>
</generator>
</id>
<property name="Content" type="BinaryBlob" not-null="true" />
<one-to-one name="ArchiveFile" class="ArchiveFile" constrained="true"/>
</class>
</hibernate-mapping>
The problem is that NHibernate doesn't use lazy loading which makes this separation useless. I've investigated the NHibernate's debug information:
Code:
Static select for entity Archiving.BusinessLayer.Entities.ArchiveFile: SELECT archivefil0_.ID as ID0_1_, archivefil0_.Date as Date0_1_, archivefil0_.FileName as FileName0_1_, archivefil0_.Type as Type0_1_, archivefil0_.ArchiveID as ArchiveID0_1_, fileconten1_.FileID as FileID1_0_, fileconten1_.Content as Content1_0_ FROM ArchiveFiles archivefil0_ left outer join FileContent fileconten1_ on archivefil0_.ID=fileconten1_.FileID WHERE archivefil0_.ID=?
Here you can see join - no lazy loading. The same story with generated select for loading FileContent - NHibernate generates join.
Do I do something wrong?
Does NHibernate support lazy loading for one-to-one associations at all?
Thanks in advance.