-->
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.  [ 7 posts ] 
Author Message
 Post subject: Load parent without childs
PostPosted: Fri Apr 27, 2007 5:34 am 
Newbie

Joined: Tue Jan 16, 2007 5:42 am
Posts: 4
Hello NHibernate users,

I have a newbie question.

I have a one-to-many relation between a parent and a child class.

There will be many childs for each parent. So, in my application, I would just show the parents, so that the application user can select the parent, and then get all childs of the selected parent ( through nhibernate ), to avoid loading of all childs of all parents.

So, my question is : how can I load the parents without loading the childs ?

Thanks in advance,

Christian

_________________
Christian Haessig
System, Network and DB administrator / DB engineer
IRCAD/EITS
mailto:christian.haessig@ircad.u-strasbg.fr


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 27, 2007 12:13 pm 
Newbie

Joined: Mon Apr 02, 2007 12:31 pm
Posts: 19
If you use lazy initialization on the collection of child objects, they won't be loaded until they're accessed. In NHibernate 1.2, this is the default behavior. In earlier versions, add lazy="true" to the collection mapping.

You should check the documentation regarding lazy initialization and proxies since there are some restrictions imposed by their use. Most notable, the collection must be mapped as an interface (ISet, IList, etc) and not a class. Also, using lazy loading can cause problems with session management; if the session that loads the parent isn't available when you attempt to load the children you will get a LazyInitializationException if you don't attach the object to the new session.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 07, 2007 12:43 pm 
Newbie

Joined: Thu Jan 20, 2005 3:03 pm
Posts: 3
I have the same problem.

Quote:
Most notable, the collection must be mapped as an interface (ISet, IList, etc) and not a class.


When you say map the collection as an interface what do you mean exactly?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 07, 2007 2:45 pm 
Beginner
Beginner

Joined: Tue May 02, 2006 8:04 am
Posts: 34
The field which actually contains the list.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 07, 2007 3:16 pm 
Newbie

Joined: Thu Jan 20, 2005 3:03 pm
Posts: 3
Hi guys, no matter what I do my child collection gets loaded. Please help.

Parent mapping file:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" schema="mySchema">
   <class name="Domain.Parent, Domain" table="PARENT" persister="Server.Persistence.NHibernateWrapper.DomainPersister, Server.Persistence.NHibernateWrapper">
      <id name="Key" type="Guid" column="ParentKey">
         <generator class="assigned"/>
      </id>
    <version name="Version" column="Version" type="Int32" unsaved-value="null" />
      <property name="Name" column="Name" type="String"/>
      <bag name="Children" lazy="true" access="Server.Persistence.NHibernateWrapper.Accessor, Server.Persistence.NHibernateWrapper">
         <key column="ParentKey" />
         <one-to-many class="Domain.Child, Domain" />
      </bag>
   </class>
</hibernate-mapping>


Child mapping file:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" schema="mySchema">
   <class name="Domain.Child, Domain" table="CHILD" lazy="true" persister="Server.Persistence.NHibernateWrapper.DomainPersister, Server.Persistence.NHibernateWrapper">
      <id name="Key" type="Guid" column="ChildKey">
         <generator class="assigned"/>
      </id>
    <version name="Version" column="Version" type="Int32" unsaved-value="null" />
      <property name="Name" column="Name" type="String"/>   
   </class>
</hibernate-mapping>


Parent class:
Code:
namespace Domain
{
   [Serializable()]
    public class Parent : IDomain
   {
      private Guid key = new Guid();       
        private string name;
        private int? version;
        private DomainCollection<Child> children;

      public Parent()
        { }
      
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public virtual int? Version
        {
            get
            {
                return version;
            }
            set
            {
                version = value;
            }
        }

        public virtual Guid Key
        {
            get
            {
                return key;
            }
            set
            {
                key = value;
            }
        }

        public DomainCollection<Child> Children
      {
         get { return children; }
            set { children = value; }
      }
    }
}


Child class:
Code:
namespace Paceart.Domain
{
   [Serializable()]
   public class Child : IDomain
   {
        private Guid key = new Guid();       
        private string name;
        private int? version;

public Child()
      { }

        public virtual string Name
        {
            get { return name; }
            set { name = value; }
        }

        public virtual int? Version
        {
            get
            {
                return version;
            }
            set
            {
                version = value;
            }
        }

        public virtual Guid Key
        {
            get
            {
                return key;
            }
            set
            {
                key = value;
            }
        }
    }
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 07, 2007 3:39 pm 
Beginner
Beginner

Joined: Tue May 02, 2006 8:04 am
Posts: 34
I don't know if it's because of the persister.

And how do you know the children are being loaded? If you 'hit' the property you will trigger the lazy initialization.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 07, 2007 3:43 pm 
Newbie

Joined: Thu Jan 20, 2005 3:03 pm
Posts: 3
Thanks, .ben. I think it is because of the persister. I took that out and ran it again, now I get a lazy exception.

I was using the SQL Profiler to verify it was loading the children.

I guess I'll dig into the persister.


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