-->
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.  [ 2 posts ] 
Author Message
 Post subject: Inheritance question
PostPosted: Sun Oct 19, 2003 5:54 pm 
Beginner
Beginner

Joined: Sat Oct 18, 2003 8:00 pm
Posts: 22
Hi all

My name is emerson and I participate of a blog project that uses struts, tiles and hibernate ([url]sf.net/projects/personalblog[/url]). I have a question that didn't manage to answer by myself :)

Currently we have Post and Comment pojos. I'm creating a downloadable pojo that will allow downloads statistics and comments of itens that can be download (it stand for pictures, articles, and whatever).

My question : What's the best way to reuse the same comment pojo and making it related to the downloadable class.

now it is : Post --1----------*--Comment
So that each comment has a link to it's related Post

How to put the downloadable in the action and related to Comments in the same way Post is?

My first solution :

Code:
Commentable --1----------*--Comment
       |
       |
______|_____
|            |
Post      Downloadable



Since there are as lot of production instances, each one has a lot of existing comments that link to existings Posts. If using my proposed solution, should I have a Commentable unique table, or two tables for both Post and Downloadable?? Will hibernate choose the right table depending of the related sub-class??

Sorry if this question is a little dumb, if it is :), some one could sign me to a place where I can find the proper solution??

Emerson Cargnin


Top
 Profile  
 
 Post subject: inheritance
PostPosted: Mon Oct 20, 2003 11:46 am 
Regular
Regular

Joined: Tue Sep 16, 2003 11:35 am
Posts: 93
Location: San Francisco, CA
Hibernate handles inheritance very well. Personally I like to use joined-subclass because it keeps the database the most orthogonal (if I have subclasses that are very similar, differing by only a field or two than I use subclass but this has limitations).

You can look in the documentation but it's really pretty simple. You have a hierarchy of tables that matches the class hierarchy. Each parent table only has the columns that all the child classes share. Hibernate is very good at correctly executing selects over the correct tables.

It would look something like this:
Code:
   <class
      name="Commentable"
      table="commentable"
      >

      <id
         name="id"
         unsaved-value="null"
         >
         <generator class="seqhilo"/>
      </id>

      <set
         name="comments"
         lazy="true"
         inverse="true"
         >

         <key
            column="commentable_id"
            >
         </key>

         <one-to-many
            class="Comment"
            />

      </set>

      <joined-subclass
         name="Post"
         table="post"
         >

         <key column="id"/>

      </joined-subclass>

      <joined-subclass
         name="Downloadable"
         table="downloadable"
         >

         <key column="id"/>

      </joined-subclass>



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