• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate: Mapping Inheritence

 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[ Posted for miguel lisboa ]

I've got an abstract class, Contrato, with two concrete subclasses, ContratoPatricular and ContratoConvencao. Can someone please show me an example of mapping these in Hibernate? Assume I have the basic properties for each of the three classes already mapped -- I just need the inheritence-hierarchy-specific tags.

Thanks!
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are many ways to map a hierarchy: all in one table, table per subclass, table per concrete class, etc. I've only used the all-in-one table approach, but the documentation and Hibernate in Action are pretty good about describing these.

My hierarchy consists of an abstract Device and two subclasses: TerminalDevice and PortableDevice. I'm using a single-column descriminator (tells Hibernate which subclass to instantiate when loading and what value to insert when persisting).PortableDevice looks just like TerminalDevice with a different discriminator-value.

Another discriminator option is to skip the value-to-classname mapping and have the column hold the classname directly. Again, the documentation is a good source for this once you have a starting example.

While "real" DBAs would cringe at the single-table method, it's very easy and not so atrocious if the number of columns not shared among classes isn't too large. Post if you have any specific questions.

Also, I hope you don't mind me posting your question for you. I don't like changing the topic of threads to ask new questions because it makes it harder for other people to learn from your questions if they can't find them.
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in fist place i want to thank you a lot for your good help
as i stated in the other post,ichanged my mind about using inheritance with Pessoa, Utente and tecnica, but i want to use it in Contrato, ContratoParticular and ContratoConvencao.
From what i read in HiA, i used table per class hierarchy(but my experiment started with peson and not with Contrato)

here are my files:

i only used this to make some basic testing and so far worked ok
my classes:

but as i said in the other post, i'll not use inheritance with these classes, but will try to use with Contrato.when i'll have concrete mappings i'll let you know
thanks again
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by miguel lisboa:
in fist place i want to thank you a lot for your good help ... i used table per class hierarchy
You're welcome! Yes, this is the correct technical name for the example I showed you above. In your example Pessoa case, it seems Pessoa is a concrete class rather than an abstract class as in my example. It looks like it should work fine as you've mapped it.

Let us know if you have any troubles.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic