-->
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: Nested Subclass & collections
PostPosted: Wed Feb 21, 2007 12:39 am 
Newbie

Joined: Tue Feb 20, 2007 11:36 pm
Posts: 8
Location: Austin TX, USA
Hibernate version: 1.2.0 Beta 3

I have a question about using Nested Subclass (One Table per class hierarchy) and collections

Here is my situation I have a three layers of derived classes (base, layer 1 and layer 2). How do I have collections of subclasses that are on layer one and will include layer 2 if I have to set a where claus for the discrimiator.

Example Hierchy
BaseShape
--SidedShape
----Square
----Triangle
--RoundShape
----Oval
----Circle

And here is my "parent" object

public class myShapes
{
public IList<RoundShape> RoundParts
public IList<Triangle> ThreeSidedParts
}

how can I populate RoundShapes properly using <subclass> with a discriminator? If I understand the 1.2.0 docs I have to set a discriminator in my where clause in the mapping file.. If I do that I can get (based on this example) Ovals or Circles but not both? or is NHibernate smart enough to know that if I use the discriminator for RoundShape it will pull both Ovals and Circles?

Thanks
Josh
PS: I hope that makes some symblance of since


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 21, 2007 10:28 am 
Regular
Regular

Joined: Tue Mar 15, 2005 12:38 pm
Posts: 73
Location: Bucharest
AFAIK you have just hit a rather weak point of NH (somebody correct me if I am wrong).

If you map:
Code:
<mapping>
   ...
   <bag name="RoundParts">
      <one-to-many class="..RoundShape.."/>
   </bag>

   <bag name="SidedParts">
      <one-to-many class="..SidedShape.."/>
   </bag>
</mapping>


On initialization NH will not create any conditions to get just the rounded shapes, it will issue a condition to fetch all BaseShape-s.
This willl bring you a nice WrongClassException because it will get at some point a sided shape in the collection of rounded shapes and this will confuse it (or vice versa)

In order to solve this I have used ranges for all subclasses such as:
Code:
BaseShape
--SidedShape 1 (1..10)
----Square   2
----Triangle 3
--RoundShape 11 (11..20)
----Oval     12
----Circle   13


Then, for the mappings you can add a where="discriminator between 1 and 10" to the collection definition to get all SidedShape-s. This will work when the collection are lazy loaded. Unfortunately, with this workaround you will not be able to fetch join this association because the where attribute value will be added to the where clause, not to the join clause.

My best advice would be to use a single collection of all objects. If you will ever need filtering you can use shape.class = MyNamespace.Triangle
in HQL to get all triangles. Criteria does not offer a similar solution, but you can map the discriminator as a read-only column and filter by discriminator.

I don't know how much you have used NH so I will also state that in most scenarios (all but the later) NH will handle the discriminator properly.

I would really like to hear some other opinions for this case!

_________________
Dragos


Top
 Profile  
 
 Post subject: so Joined-Subclass then?
PostPosted: Wed Feb 21, 2007 10:55 am 
Newbie

Joined: Tue Feb 20, 2007 11:36 pm
Posts: 8
Location: Austin TX, USA
So should I consider using Joined-Subclasses then? I know those work in this kinda situation (or atleast they did in 1.0.3 on a test case I ran)..

What kind of performance hit would I take using <Joined-Subclass>
instead of <subclass> on a LARGE dataset (say 500,000 parent objects with 2 collections of 10 child objects each so 10,000,000 child objects)?
Or is that using the wrong "fix" for the stated problem?

Thanks again
Josh


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 21, 2007 11:14 am 
Regular
Regular

Joined: Tue Mar 15, 2005 12:38 pm
Posts: 73
Location: Bucharest
I would not bet that joined subclass will not suffer from the same behavior. But if you have test cases for both initialization from lazy instance and fetch joining that I take it for granted.

As from the performance perspective it is not about the size it's just about the approach. That is retrieving collections with separate selects or with joins, all at a time or one at a time. I would say though that left joins may lower performance a bit (it depends also what database you are using) but not significantly. If you have the hierarchy on 7 tables you will always get 6 left joins but these can be optimized.

Unfortunately, I don't really think that there is a fix but just workarounds for this. Best approach (as in avoiding headaches) is to use a single collection holding all types. This is not a good OOD decision but than again you can't have them all :).

The main issue is that, at the moment, NH cannot add conditions on join clauses so anyhow you take it you will find some bumpy roads. I have used the discriminator approach but I now wish I would have used a single collection for all (because I didn't I can't really bet all my money on this solution either).

In conclusion, try using joined classes, make some tests using all kind of data with all types of fetching and than take the decision you find to suit you best. AND, don't forget to give us you feedback on the approach you take.

_________________
Dragos


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.