-->
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.  [ 9 posts ] 
Author Message
 Post subject: Loading subsets of the full object graph
PostPosted: Mon Jun 28, 2004 5:27 pm 
Beginner
Beginner

Joined: Thu Jun 24, 2004 1:04 pm
Posts: 35
Location: Minnesota - USA
Almost there to getting an initial conversion of our persistence infrastructore over to Hibernate...

But, I'm totally stuck now. I'd just assumed this was possible, and now that I'm to the point I can actually try to do it, I cannot seems to figure it out. It's a problem about loading a subset graph instead of the full object graph.

I only post because I'm at my wits end, I've spent better part of today scanning the manual, all the FAQs, and searching the forums for some idea of a solution (or at least explanation).

Let me provide a simple example. Given the following kind of model:

    A 1:m B (A has 1:m relation to B)
    B 1:m C (B has 1:m relation to C)
Assume POJOs with appropriate collection properties for these relations (middlegen + hbm2java). All relations are defined to be lazy=true.

The full object graph is then defined as (using inner joins)...

    Given an A, load all B related to that A, and load all C related to each B.
There are various ways of loading this graph, Hibernate has tons of support for this. No problems here!

But, I want to be able to get a subset of this object graph, say...

    Given an A satsifying condition Q1, load all B related to that A satisfying condition Q2, and load all C related to each such B satisfying condition Q3.

And I want this in the exact same POJO model already defined.

I'm able to load A, and I'm trying to use 'join fetch' to go from A -> B, but I cannot do the same from B -> C (current limitation of Hibernate, 1 fetch per query). The documenation also says I should not (cannot?) use a WHERE clause, but the SQL seems to use it.

Now, even if I partially load this graph up. As soon as I try to use A->B, an initialization occurs (I've defined all relations to be lazy=true, per recommendations).

Ideas?

--gus


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 28, 2004 5:30 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Use an HQL or Criteria query to express the fetch strategy for A and all B's, that should be no problem, even with a restriction. I don't see a way to fetch all C's at the same time, using a single outer-join query. Think how huge the result set will be. You will have to use two queries.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 28, 2004 5:32 pm 
Beginner
Beginner

Joined: Thu Jun 24, 2004 1:04 pm
Posts: 35
Location: Minnesota - USA
Also I'm using cascade="all" (In particular, not using all-delete-orphan) --gus


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 28, 2004 5:34 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
The cascading options (we named that "transitive persistence") has nothing to do with _fetching_, it's purpose is automated lifecycle transitions on associations.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 28, 2004 5:35 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
cascade=all doesn't have anything to do with "loading"

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 28, 2004 5:37 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
oups sorry christian, we've replied at the same time.

gus, just to answer your question, as Christian said just call 2 "fetch" queries
from A as a fetch join a.bs
and
from A as a fetch join a.cs

and your object graph will loaded in 2 queries (better than N+1)

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 28, 2004 5:39 pm 
Beginner
Beginner

Joined: Thu Jun 24, 2004 1:04 pm
Posts: 35
Location: Minnesota - USA
I'm using this HQL currently:

Code:
      String hql =
         "from SystemCode systemCode"
            + " join fetch systemCode.subCodes subCode"
            + " join fetch subCode.subCodeData subCodeDatum"
            + " where systemCode.code = :code"
            + " and subCode.comp_id.subCode = '_DATA_DEFINITION'"
            + " and subCodeDatum.comp_id.dataElementName != '_DESCRIPTION'"
            + " order by subCodeDatum.comp_id.dataElementName asc";


Let's say I do load A->B. How do I go from B->C for each B using a HQL/query with a restriction?

Can I do a query for B->C and then do a B.setC(results)? Will that have any bad side-effects that'll cause deletes (on exclude C's) or spurious updates?

--gus


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 28, 2004 5:42 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
You should try collection filters.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 28, 2004 5:47 pm 
Beginner
Beginner

Joined: Thu Jun 24, 2004 1:04 pm
Posts: 35
Location: Minnesota - USA
You guys post so darn fast. ;)

I'll try using B.setC(filter(C)) and see how that goes.

Thanks!

--gus


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