-->
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: Not "Cascade" bags when loading an object. Is it p
PostPosted: Fri Feb 15, 2008 9:49 am 
Newbie

Joined: Fri Apr 20, 2007 1:46 pm
Posts: 5
Hi all,

In our project we have users that belongs to solution groups. We need the information of both which users belongs to a given solution group and also the solution groups of a given user. Therefore we are using bags on both user and solution (many-to-many relationships) to bring this information.

The problem is when bringing the user information we don't need to bring all the users that belongs to its solution groups (what is happening on our approach).

Is there any way of avoiding this cascade? As we are not keeping our NHibernate sessions we can not use lazy loading (as far as I know, but please correct me if I'm mistaken).

Thanks a lot!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 15, 2008 4:30 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
First, terminology: that is not "cascading" but eager fetching of collections. "Casade" refers to propogation of certain commands to associated object: see http://www.hibernate.org/hib_docs/nhibe ... ata-graphs .

Eager fetching of an object graph through a recursive relationship is precisely the kind of problem that lazy-loading is meant to solve. Even if you can't keep your session, you should be able to lock to a new session before accessing the collections, or eagerly fetch just the collections you need before discarding your session. There are a fair number of options, but disabling lazy-loading doesn't admit much flexibility.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 15, 2008 6:01 pm 
Newbie

Joined: Fri Apr 20, 2007 1:46 pm
Posts: 5
Hi marcal,

First of all still learning hibernate/Nhibernate so sorry about the misused terminology.

Could you please post me (or indicate a good example) on how can I do this eager fetching within my session? It may solve the problems we are facing...

Thanks a lot!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 16, 2008 1:25 am 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
Sure. First, you have the most options when you leave your collections lazy. Then, you can decide which the best fetch option is at design- (or possibly even run-) time.

In the docs: a good primer is here: http://www.hibernate.org/hib_docs/nhibe ... e-fetching .

HQL syntax: http://www.hibernate.org/hib_docs/nhibe ... yhql-joins

ICriteria syntax: http://www.hibernate.org/hib_docs/nhibe ... icfetching

Hopefully that gives you a good start!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 16, 2008 5:06 pm 
Newbie

Joined: Fri Apr 20, 2007 1:46 pm
Posts: 5
Hi,

First of all thanks for the reference. I'm facing now a problem trying to use the lazy-fetched collection.

I've defined:

Code:
    <bag name="SolutionGroups" table="..." lazy="true" cascade="none">
      <key column="..." />
      <many-to-many class="..." column="..." />
    </bag>


In order to Initialize it within my Session I've tryied:

Code:
     NHibernateUtil.Initialize(returnValue.SolutionGroups);


I've also tryied to initialize each element within the collection. Using NHibernateUtil.IsInitialize for the list I got true (within Session).


The problem is, after closing the session and try to use the collection (on my presentation layer)

Code:
   IList solutionGroups = user.SolutionGroups;
    for (int i = 0; i < solutionGroups.Count; i++)
                {...


I still recieve the LazyInitializationException. Debugging it I could check IsInitialize returns me false there, altought it was initialize before (and IsInitialize had returned true).

What have I missed?


Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 18, 2008 3:10 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
Haven't seen that problem... what happens if you use eager fetching instead of post-fetch initialization?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 19, 2008 8:41 am 
Newbie

Joined: Fri Apr 20, 2007 1:46 pm
Posts: 5
Hi,

Using eager fetching I can retrieve the object properly but it also brings all the solution groups AND the solution groups' users (what is not used, so it is just being a performance bottleneck).

Is it possible to intercept the lazy fetching somehow so I can open a new Session, retrieve data and close it?

Thanks,


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.