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.  [ 3 posts ] 
Author Message
 Post subject: Need help with lazy loading
PostPosted: Fri Apr 11, 2008 10:00 am 
Newbie

Joined: Sat Jul 01, 2006 3:37 pm
Posts: 7
I'm sorry if this post already exists somewhere but I read that the search tool was not working and I realy need help with this one.

I got this situation. I have 2 classes : Campaign, Customer with share a relation one-to-many. So I created this mapping

Code:
<class name="Campaign" table="campaign" lazy="true">
<id name="campaignName" column="campaignname" type="String">
<generator class="assigned">
</id>

<bag name="Customers" cascade="all-delete-orphan" lazy="true">
<key column="campaignname">
<one-to-many class="Customer" />
<bag>

<property column="Description" type="String" name="Description" not-null="true" length="31" />
</class>

<class name="Customer">
<composite-id access="field" >
<key-property name="CampaignName" column="campaignname" type="String"/>
<key-property name="FirstName" column="firstName" type="String"/>
</composite-id>

<many-to-one name="Campaign" class="Campaign" column="campaignname" />

<property column="LastName" type="String" name="LastName" not-null="true" length="15" />
</class>


In my c# classes, I created an IList<Customer> in my Campaign object in order to have my collection of Customers. I also created a Campaign object in my Customer class to have access to my campaign at my customer level.

I faced now this issue :

When I try to lazy load my campaign information from my customer object it works. ie:
Code:

                ICriteria criteria = session.CreateCriteria(typeof(Customer));
                criteria.Add(Expression.Eq("CampaignName", "ES3000J2"));
                criteria.Add(Expression.Eq("FirstName", "Julian"));
                List<Customer> list = (List<Customer>)criteria.List<Customer>();

                if (list.Count > 0)
                {
                    Console.WriteLine("Customer Last Name: " + list[0].LastName);
                    Console.WriteLine("Campaign desc : " + list[0].Campaign.Description);
                }



But when I tried to access my customers from my campaign it did not work :

Code:

Campaign test = session.Get<Campaign>("ES3000J2");
Console.WriteLine("#customer : " + test.Customers.Count);



Customers.Count is always 0

What am I missing ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 11, 2008 11:33 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Just a hunch, mark the collection as inverse as you have a bidirectional association.

<bag name="Customers" inverse="true" cascade="all-delete-orphan" lazy="true">
<key column="campaignname">
<one-to-many class="Customer" />
<bag>
Code:

If that does not help, just to make sure, is it working when you set lazy to false ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 11, 2008 1:09 pm 
Newbie

Joined: Sat Jul 01, 2006 3:37 pm
Posts: 7
Hi, thanks for your quick answer.
Actually none of those modification worked... :-(

I tried those configuration

Code:
<bag name="Customers" cascade="all-delete-orphan">
<bag name="Customers" lazy="false" cascade="all-delete-orphan">
<bag name="Customers" inverse="true" lazy="false" cascade="all-delete-orphan">
<bag name="Customers" inverse="true" lazy="true" cascade="all-delete-orphan">


I also verified the SQL genereted by NHibernate and it retreves the information so I realy don't understand why it's not working


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