-->
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.  [ 4 posts ] 
Author Message
 Post subject: Associated objects being proxies
PostPosted: Tue Oct 02, 2007 8:21 am 
Regular
Regular

Joined: Fri May 05, 2006 11:54 am
Posts: 51
Hi all,

This post accurately describes the problems I am having
http://forum.hibernate.org/viewtopic.php?t=977139&highlight=cproxy

I have an object contact that has four many-to-one relationships. If these associated objects are in the session cache then I am in trouble.
I need these associated objects to be not proxies.
To cut a long story short, I need to somehow initialize the contact and all it’s associated objects.

Is there a short way of doing this, I have resorted to creating a multi part query each time I want to load a contact like so:

Code:
ISession session = _sessionManager.OpenSession();
IQuery contactQuery = session.CreateQuery("from Contact c where c.Uid = :contactuid");
contactQuery.SetGuid("contactuid", contactUid);
string hql = "select co.Company from Contact co where co.Uid = :comanycontactuid";
IQuery companyQuery = session.CreateQuery(hql);
companyQuery.SetGuid("comanycontactuid", contactUid);
IQuery businessUnitQuery =
    session.CreateQuery("select co.BusinessUnit from Contact co where co.Uid = :businessunituid");
businessUnitQuery.SetGuid("businessunituid", contactUid);
IQuery siteQuery = session.CreateQuery("select co.Site from Contact co where co.Uid = :siteuid");
siteQuery.SetGuid("siteuid", contactUid);
IQuery businessFunctionQuery = session.CreateQuery("select co.BusinessFunction from Contact co where co.Uid = :bfcontactuid");
businessFunctionQuery.SetGuid("bfcontactuid", contactUid);
IList results = session.CreateMultiQuery()
    .Add(contactQuery)
    .Add(companyQuery)
    .Add(businessUnitQuery)
    .Add(siteQuery)
    .Add(businessFunctionQuery)
    .List();
Contact ret = ObjectFromArrayList<Contact>(results[0]);
ret.Company = ObjectFromArrayList<Company>(results[1]);
ret.BusinessUnit = ObjectFromArrayList<BusinessUnit>(results[2]);
ret.Site = ObjectFromArrayList<Site>(results[3]);
ret.BusinessFunction = ObjectFromArrayList<BusinessFunction>(results[4]);
return ret;


Surely there has to be a better way than this?

Cheers


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 02, 2007 9:49 am 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
With HQL, you should be able to do this:
Code:
from Contact c
    join fetch c.Company
    join fetch c.BusinessUnit
    join fetch c.BusinesFunction

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 02, 2007 10:08 am 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
And with criteria you should be able to use SetFetchMode(associationName, FetchMode.Join) on ICriteria for each of the associations you want to load.


Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 02, 2007 10:21 am 
Regular
Regular

Joined: Fri May 05, 2006 11:54 am
Posts: 51
Both the above examples still give me a proxied association object.

I would have thought NHibernateUtil.Initialize might have done something but it does not do what I want.

Can anyone think of anything else?

Thanks

Paul


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