-->
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.  [ 5 posts ] 
Author Message
 Post subject: one-to-one lazy association
PostPosted: Fri Mar 21, 2008 11:46 am 
Newbie

Joined: Fri Mar 21, 2008 11:25 am
Posts: 2
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.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 21, 2008 1:52 pm 
Beginner
Beginner

Joined: Fri Aug 10, 2007 3:34 am
Posts: 44
the one-to-one mapping element has a lazy property. try setting lazy="true"
in the mapping document


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 21, 2008 2:26 pm 
Newbie

Joined: Fri Mar 21, 2008 11:25 am
Posts: 2
Thank you for reply.

In NHibernate 1.2 all types are lazy by default. And 'lazy' attribute has two possible values: 'false' and 'proxy'. Obviously 'false' disables lazy behaviour. I've tried to use 'proxy' - no luck.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 22, 2008 7:20 am 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
The simple answer is that no, lazy one-to-one is not supported. There are several posts on the subject:

http://forum.hibernate.org/viewtopic.php?t=975957
http://forum.hibernate.org/viewtopic.php?t=969713

There have been several who have attempted to work around the limitation, but believe me, in that direction lies madness. :)

Cheers,

Symon.

_________________
Symon Rottem
http://blog.symbiotic-development.com


Top
 Profile  
 
 Post subject: Re: one-to-one lazy association
PostPosted: Wed May 13, 2009 9:59 am 
Newbie

Joined: Tue Dec 30, 2008 6:20 pm
Posts: 4
Location: Bulgaria
I succeed to use lazy with OneToOne using @OneToOne(fetch=FetchType.LAZY, mappedBy="bpsCompByPkFkCopoCompId", optional=false)
To avoid the non-null check for nullable OneToOne relationships I had to modify the Hibernate class
org.hibernate.engine.Nullability.checkNullability(){
....
if ( !nullability[i] && value == null ) {
if (!(propertyTypes[i] instanceof OneToOneType)) {//do not throw any exception if type is OneToOne
//check basic level one nullablilty
throw new PropertyValueException( "not-null property references a null or transient value",
persister.getEntityName(), persister.getPropertyNames()[i]);
}

}
}
In this way I leave the non-null check on the underlying Database.
The performance I achieved using OneToOne instead OneToMany is that when iterating through the results of lazy joined onetoone entities, All lazy joined entities was fetched via one big batch select instead of many single selects per each entity(case with OneToMany).
What do you think about this solution?
Any ideas?

_________________
Nikolai Gagov


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