Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mapping confusion..

 
Dave Brown
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

New at using OR Mapping but something I want to pursue however i'm having a mental block trying to relate a schema to a Mapping or vice versa..

E.g.

Say I start creating my objects first and want to create the schema from my objects. and have this scenario...

A animal class which consists of
String type
int Age
int HairColor

Now I get confused when I want to also create subclasses e.g.

dog class or cat class
which both extend animal but have their own fields also...

I dont see how a scema mapping can be generated when using subclasses...

Any thoughts ?
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using Hibernate? Section 4.2 and 9 of the reference documentation talk about inheritance. Maybe that will get you going.
 
Dave Wood
bronco
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave,

Often the easiest thing to do in this situation is just add the subtype fields to your database table and then add an additional column that indicates the actual type of the object.

So, for example, your ANIMAL table might have these columns:

animal_id
type
age
haircolor
dogattribute
catattribute

...where "type" is some indication of "dog" vs. "cat" and the dogattribute/catattribute columns are null when not applicable. By the way, the fact that you already have a "type" attribute in your Animal class is a little disconcerting. The object type itself should be all you need (having the subtype stored as a string in the superclass is typically a sign of iffy design).

HTH,
-dave

PS: There are certainly other ways to handle this in the DB. The Hibernate docs mentioned previously cover all of the common approaches.
 
Dave Brown
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh so its not such a problem after all.

Thanks for the replies. I'm not too keen on keeping all fields in one table though, Wouldnt there be performance gains keeping seperate tables, and if I wanted to add a new class later its own table makes sense. My background comes from file based database systems, e.g each index in a seperate file , the data in a file, data structure in anoher file... So thats probably the origins of my reluctance to use one table.

Dave.
 
Dave Wood
bronco
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, there are certainly trade-offs. A lot depends on how many other things link to "Animal." If you've got a Dog table and a Cat table and you want your Humans to have an "Animal" foreign key, it gets a little tricky because you want to allow that FK to reference either your dog table OR your cat table...not something FK's really like to be asked to do.

Then there's the option of having an Animal table AND a Cat table AND a Dog table. Also a potentially reasonable option, but now you're introducing joins just to populate any given Animal object.

-dave
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic