-->
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: Tree structure and subclassed child elements
PostPosted: Mon Jun 08, 2009 6:07 am 
Beginner
Beginner

Joined: Wed Dec 10, 2008 5:59 am
Posts: 47
I have a tree structure where the class HierarchyNode acts as a base class. Furthermore, i have three classes (HierarchyRootNode, AlbumHierarchyNode, TrackHierarchyNode) which extends HierarchyNode.

My tree structure could look as follows:

- HierarchyRootNode
-- AlbumHierarchyNode
---- AlbumHierarchyNode
---- AlbumHierarchyNode
-- TrackHierarchyNode
---- TrackHierarchyNode
---- TrackHierarchyNode

The problem:

The AlbumHierarchyNode has a collection "albums". Each album element has a collection "territories", which indicated in which territories the album can be distributed.
Now; how would i go about to load the entire tree structure - but only the albums for each AlbumHierarchyNode which belongs to a provided territory.

I've been looking high and low for a solution on this, but i can't really seem to come up with a working one...!

Any help highly appreciated.

Mapping:

Code:
<hibernate-mapping>
  <class name="com.tracker.mediacreator.db.entities.HierarchyNode" table="hierarchynode">
    <id column="id" length="36" name="id" type="com.tracker.mediacreator.db.usertypes.CustomUUID"/>
    <discriminator column="type" type="java.lang.String"/>
    <version column="updated" name="updated" type="java.util.Calendar" unsaved-value="null"/>
    <property column="name" length="200" name="name" type="java.lang.String"/>
    <many-to-one class="com.tracker.mediacreator.db.entities.HierarchyNode" column="parent_id" lazy="false" name="parent"/>
    <property column="status" length="1" name="status" not-null="true" type="java.lang.Integer"/>
    <property column="flag" length="1" name="flag" not-null="true" type="java.lang.Integer"/>
    <bag fetch="subselect" inverse="true" lazy="false" name="children" order-by="name asc">
      <key column="parent_id"/>
      <one-to-many class="com.tracker.mediacreator.db.entities.HierarchyNode"/>
    </bag>
    <subclass discriminator-value="ROOT" name="com.tracker.mediacreator.db.entities.HierarchyRootNode"/>
    <subclass discriminator-value="TRACK" name="com.tracker.mediacreator.db.entities.TrackHierarchyNode">
      <bag fetch="subselect" inverse="true" name="tracks">
        <key column="hierarchynode_id"/>
        <one-to-many class="com.tracker.mediacreator.db.entities.TrackHierarchyRelationship"/>
      </bag>
    </subclass>
    <subclass discriminator-value="ALBUM" name="com.tracker.mediacreator.db.entities.AlbumHierarchyNode" >
      <bag fetch="subselect" inverse="true" lazy="false" name="albums">
        <key column="hierarchynode_id"/>
        <one-to-many class="com.tracker.mediacreator.db.entities.AlbumHierarchyRelationship"/>
      </bag>
    </subclass>
  </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: Re: Tree structure and subclassed child elements
PostPosted: Mon Jun 08, 2009 6:39 am 
Beginner
Beginner

Joined: Mon Jun 01, 2009 5:39 am
Posts: 34
IMHO, the main problem here is that you reason in term of objects and not in term of the database itself.

Imagine you'd have this table, along with a table albums and tracks:

Code:
table: album_distributions
territory varchar(255)
album_id varchar(255)
unique index on (territory, album_id) -- note the order of columns in the index!


Then you could:

Code:
select * from albums a left join album_distributions ad on a.id = ad.album_id where ad.territory = :thegiventerritory


The schema which you wish to use would be helpful here. But please remember not to neglect the 'R' in "ORM". It is probably the most important part to my eyes.


Top
 Profile  
 
 Post subject: Re: Tree structure and subclassed child elements
PostPosted: Mon Jun 08, 2009 8:11 am 
Beginner
Beginner

Joined: Wed Dec 10, 2008 5:59 am
Posts: 47
Hi,

Thanks a lot for your reply. I could, of course traverse the entire tree and retrieve albums for each AlbumHierarchyNode based on a given territory
through an HQL query. However, i was more hoping to be able to have Hibernate to take care of loading the correct albums through subselects, by using
the Criteria API to specify which territory to filter albums on.

However; since i'm dealing with different classes (AlbumHierarchyNode and TrackHierarchyNode) - it seems that this can't be done through
the Criteria API. This is because the two classes have different properties, and as a result of this an query tailored to fetch only
certain albums for AlbumHierarchyNodes would crash when Hibernate fetches TrackHierarchyNodes (?).


Top
 Profile  
 
 Post subject: Re: Tree structure and subclassed child elements
PostPosted: Mon Jun 08, 2009 8:28 am 
Beginner
Beginner

Joined: Mon Jun 01, 2009 5:39 am
Posts: 34
sbrattla wrote:
Hi,

Thanks a lot for your reply. I could, of course traverse the entire tree and retrieve albums for each AlbumHierarchyNode based on a given territory
through an HQL query. However, i was more hoping to be able to have Hibernate to take care of loading the correct albums through subselects, by using
the Criteria API to specify which territory to filter albums on.

However; since i'm dealing with different classes (AlbumHierarchyNode and TrackHierarchyNode) - it seems that this can't be done through
the Criteria API. This is because the two classes have different properties, and as a result of this an query tailored to fetch only
certain albums for AlbumHierarchyNodes would crash when Hibernate fetches TrackHierarchyNodes (?).


Well, am I correct in assuming that an Album can have several Tracks?

If yes, why not define a one-to-many relation between Album and Tracks? You don't even need to inherit them from a common class - in fact, you should NOT do so.

If you then build an HQL query such as:

Code:
from albums a left join album_distributions ad on a.id = ad.album_id where territory = :thegiventerritory


you will then obtain a List of Album POJOs with the Tracks for this Album available through the Album object itself, unless you have selected to lazy-load the Tracks.


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.