Hello,

Recently I've been using mappings with named sql queries.
They include left joins and "load-collection" elements. Lazy loading
is enabled.

With this setup, I have seen that NHibernate creates uninitialized
proxies for collections for which the query result indicates 0
elements. Is it the right behaviour? I was expecting a regular empty
collection and current behaviour seems like a bug to me.


I'm using the following mapping:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                                   assembly="NHibernate.Test"
                   namespace="NHibernate.Test.NHSpecificTest.NH1234"
                   default-access="property"
                                   default-lazy="true">

  <class name="Parent">
    <id name="Id">
      <generator class="guid.comb" />
    </id>
          <set cascade="save-update" inverse="true" name="Children">
                  <key>
                          <column name="Parent_id"/>
                  </key>
                  <one-to-many class="Child" />
          </set>
  </class>

        <class name="Child">
                <id name="Id">
                        <generator class="guid.comb" />
                </id>
                <many-to-one class="Parent" name="Parent">
                        <column name="Parent_id" not-null="true" />
                </many-to-one>
        </class>

        <sql-query name="AllParentsAndChildren">
                <load-collection alias="c" role="Parent.Children"/>
                <return class="Parent" alias="p"/>
<![CDATA[
                SELECT
                        {p.*}, {c.*}
                FROM
                        Parent p
                        LEFT JOIN Child c ON p.Id = c.Parent_id
]]>
        </sql-query>
</hibernate-mapping>


And the following test:

[Test]
        public void
CollectionsLoadedFromSqlQueryShouldBeInitializedAlsoWhenTheyAreEmpty()
        {
            using (var session = OpenSession())
            {
                                using (var tx = session.BeginTransaction())
                                {
                                        var parentWithChildren = new Parent();
                                        parentWithChildren.Children.Add( new 
Child { Parent =
parentWithChildren } );

                                        var parentWithoutChildren = new 
Parent();

                                        session.Save( parentWithChildren );
                                        session.Save( parentWithoutChildren );
                                        session.Flush();
                                        session.Clear();


                                        // load
                                        var query = session.GetNamedQuery( 
"AllParentsAndChildren" )
                                                .SetResultTransformer( 
Transformers.DistinctRootEntity );

                                        var parents = query.List<Parent>();

                                        var parentWithChildrenFromDb = 
parents.Where( x => x.Id ==
parentWithChildren.Id ).Single();
                                        var parentWithoutChildrenFromDb = 
parents.Where( x => x.Id ==
parentWithoutChildren.Id ).Single();

                                        // this passes correctly
                                        Assert.IsTrue( 
NHibernateUtil.IsInitialized
( parentWithChildrenFromDb.Children ) );

                                        // fails on this one
                                        Assert.IsTrue( 
NHibernateUtil.IsInitialized
( parentWithoutChildrenFromDb.Children ), "Collection should be
initialized." );


                                        // cleanup
                                        tx.Rollback();
                                }
            }
        }


I have created a test case for this at
https://sites.google.com/a/dorochowicz.com/artur-public-files/files-1/NHibernate.Test2.zip?attredirects=0


Kind regards
Artur Dorochowicz
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"nhusers" 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/nhusers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to