-->
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.  [ 3 posts ] 
Author Message
 Post subject: Modelling a collection of instances of an abstract class
PostPosted: Mon Jun 05, 2006 11:13 am 
Newbie

Joined: Mon Jun 05, 2006 10:41 am
Posts: 2
Question from a colleague:

> > > Can anyone point me to an example of a mapping file which illustrates how to
> > > model a collection of instances of an abstract class?
> > >
> > > The excellent article on the visitor pattern discusses the java side in detail,
> > > see http://www.hibernate.org/280.html, but I would like to know what the
> > > mapping of a collection of polymorphic objects would look like.
> > > For example, if I had a class called Congress I would like to add an attribute
> > > 'members' which contains a list of Politian objects. In the model I don't
> > > care whether they are Democrat, Republic or just Lazy Politiions. That is a
> > > run time issue. I will probably be using the one relation per concrete class
> > > approach for my inheritance model. So I need a way to tell Hibernate how to
> > > gather selections from several differnent tables into a single list that can
> > > be processed by applying abstract methods.
> > >
> > > If the answer is the manual please just point me to the relevant page.

Regards,


Top
 Profile  
 
 Post subject: Re: Modelling a collection of instances of an abstract class
PostPosted: Mon Jun 05, 2006 6:49 pm 
Beginner
Beginner

Joined: Tue Oct 18, 2005 3:57 pm
Posts: 48
Location: Los Angeles, CA
Quote:
> > > I will probably be using the one relation per concrete class approach for my inheritance model.

You mean table-per-subclass?

Quote:
> > > Can anyone point me to an example of a mapping file which illustrates how to model a collection of instances of an abstract class?

If you want table-per-subclass, try this --

Schema:
Code:
TABLE POLITICIAN ( P_ID <PK>, NAME, ... )
TABLE REPUBLICAN ( REP_ID <PK>, ... )
TABLE DEMOCRAT ( DEM_ID <PK>, ...)

where REP_ID and DEM_ID are also foreign key references to P_ID.

Mapping:
Code:
<id name="id" column="ID">
    ...
</id>
<property name="politicianName" column="NAME" ... />

<joined-subclass name="Republican" table="REPUBLICAN">
    <key column="REP_ID"/>
    <property .../>
    ...
</joined-subclass>

<joined-subclass name="Democrat" table="DEMOCRAT">
    <key column="DEM_ID"/>
    <property .../>
    ...
</joined-subclass>

The <joined-subclass> tag maps a subclass to a new table. All props declared in the subclass will be in this table. A Republican object lookup will require a join of both tables (POLITICIAN + REPUBLICAN) based on the IDs.


Top
 Profile  
 
 Post subject: Re: Modelling a collection of instances of an abstract class
PostPosted: Mon Jun 12, 2006 7:07 am 
Newbie

Joined: Mon Jun 05, 2006 10:41 am
Posts: 2
OK I can see I have not made it completely clear what I am trying to do.
I can see that I will need some tables to represent the base class and
the derived classes and that using the joined-subclass construct as you
have suggested will solve that part of the problem nicely. So thanks for
that example:- it is useful. However, to complete the story I am looking
for an example that shows what would be required to generate a selection
that retrieves a set of various polititians (Democrat, Republican etc).
In particular, I would like to see how Hibernate supports the scenario of
adding a few more types of polititian, e.g. Green, Idle, and Looney.
A typical application would be written to use the generic Politition
interface to these classes and would need very little alteration outside
the DB layer.

Here are two examples which might occur in the same application:-

1. A government is made up of politians and exists for a period of time:-

class Gov
{
ID id;
Politian[] main_party;
Politian[] opposition;
Date start_date;
Date end_date;
}

How should I generate a method which will return a list of the politions in
the opposition, which may include more than one class of polition?

2. We may want to record how each politian in a government voted on a particular act:-

class Vote
{
ID id;
String act;
Politian[] voted_for;
Politian[] voted_against;
Politian[] abstained;
}

How should I write the mapping so that I can get a list of Politians that
abstained from voting in a given act? (So that I can call a method such as
MarkAsUseless()).

I hope this makes what I am trying to do more lucid without introducing to
many terms from UK politics to confuse matters.


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