-->
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: How expensive is a collection that is rarely used?
PostPosted: Sat Dec 18, 2004 3:14 am 
Beginner
Beginner

Joined: Tue Sep 09, 2003 5:20 pm
Posts: 43
Hibernate version:
2.1.7

This is more of a question than a problem or issue. I've read Hibernate in Action and the manual, but did not see a clear answer to this.

I've got a somewhat deep class hierarchy, with five classes A, B, C, D, E, F and G, such as:
Code:
          A                       G
          / \                     
         /   \                     
        B     C
            / |\
          /   |  \
          D   E   F


Mapping documents:
Code:
    <class  name="A">
        <id name="id"><generator class="native"></generator></id>
        <set name="Gs" table="G" lazy="true" inverse="true">
              <key column="a-id"/>
              <one-to-many class="G"/>
        </set>
    </class>
    ...
    <class  name="G">
        <id name="id"><generator class="native"></generator></id>
        <many-to-one name="parent" class="A"cascade="none" outer-join="false"></many-to-one>
     </class>


Background/Requirements:
- Class G has a many-to-one reference to any A or subclass of A. However in reality only instances of class B and E will be referenced.
- I need to access a collection of Gs from instances of subclass B and E.
- Instances of B and E are a very small minority of the total instances - way less than one percent.

Question:
How much overhead does it cost to handle the non-existent collections for all subclasses of A? It will be somewhat messy if I have to create two variations of G, one for B and one for E.

Thank you - Richard


Top
 Profile  
 
 Post subject: Re: How expensive is a collection that is rarely used?
PostPosted: Sat Dec 18, 2004 12:18 pm 
Regular
Regular

Joined: Thu Oct 07, 2004 4:45 pm
Posts: 92
rnmixon wrote:
- Class G has a many-to-one reference to any A or subclass of A. However in reality only instances of class B and E will be referenced.
- I need to access a collection of Gs from instances of subclass B and E.


I don't understand your relationships. A to G is a one-to-many, but there are also one-to-many relations from B and E to G? Why not a many-to-many between A and G? And what are the "non-existent" collections?


Top
 Profile  
 
 Post subject: Re: How expensive is a collection that is rarely used?
PostPosted: Sat Dec 18, 2004 5:35 pm 
Beginner
Beginner

Joined: Tue Sep 09, 2003 5:20 pm
Posts: 43
rhasselbaum wrote:
rnmixon wrote:
- Class G has a many-to-one reference to any A or subclass of A. However in reality only instances of class B and E will be referenced.
- I need to access a collection of Gs from instances of subclass B and E.


I don't understand your relationships. A to G is a one-to-many, but there are also one-to-many relations from B and E to G? Why not a many-to-many between A and G? And what are the "non-existent" collections?


Let me try to explain better.

Any A or subclass of C can theoretically have a collection of G's. The G's have a many-to-one relationship to an A or a subclass of A.

The issue is that here are millions of instances of D and F, but only a few thousand instances of B and E. Because they all inherit from A, A is the first choice for placing the collection of G's. Then both the B's and E's inherit the behaviour I will program into this collection. Otherwise I have to define two variations of G, one that has a many-to-one pointing at B and another tha ha a many-to-one pointing at E.

The other approach I'm starting to consider is to define a separate entity that owns the collection of G's. Then just have an optional one-to-one between either B or E and this new entity.

Any thoughts? Thanks - Richard


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 19, 2004 7:01 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
the only thing that will happen is that you will get an empty set for those who don't have any elements.

if you mark the set as lazy then it won't be more than a construction of a set, which isn't a big deal (IMHO)

this lazy set won't cost anything if you dont access it (where it will have to go check if there are some objects associated with it...this check performance will depend on your indexes)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 19, 2004 1:39 pm 
Beginner
Beginner

Joined: Tue Sep 09, 2003 5:20 pm
Posts: 43
Max, thank you for the response. I was hoping that was the case. Now I just need to figure out if creating that empty set is really "significant" to my application. I tend to think not - but its going to happen millions of times a day. Its certainly not elegant, but I do not see a really easy way of refactoring to avaoid this.

Thanks again - Richard


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 19, 2004 1:41 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
a million times is many ;)

Maybe you should consider NOT having a set, but use a query to get that information instead ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 20, 2004 1:49 pm 
Beginner
Beginner

Joined: Tue Sep 09, 2003 5:20 pm
Posts: 43
Duh! Of course.

I was too focused on using the very nice collection semantics that hibernate promotes. I will add a utility method to my DAO and all will be fine.


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.