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.
posted 15 years ago
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.
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
posted 15 years ago
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.
If you look closely at this tiny ad, you will see five bicycles and a naked woman: