Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ORM Inheritance

Has anyone really wanted and used inheritance support on ORM tools, and if yes which one do you think offers the best support?

Or is ORM Inheritance a "pie in the sky" concept?

like image 317
Otávio Décio Avatar asked Feb 27 '09 14:02

Otávio Décio


People also ask

What is inheritance in hibernate?

Entity inheritance means that we can use polymorphic queries for retrieving all the subclass entities when querying for a superclass. Since Hibernate is a JPA implementation, it contains all of the above as well as a few Hibernate-specific features related to inheritance.

What are the inheritance mapping strategies?

There are three inheritance mapping strategies defined in the hibernate: Table Per Hierarchy. Table Per Concrete class. Table Per Subclass.

What is a JPA inheritance?

JPA Inheritence Overview Inheritence is a key feature of object-oriented programming language in which a child class can acquire the properties of its parent class. This feature enhances reusability of the code. The relational database doesn't support the mechanism of inheritance.

How many strategies are there in hibernate inheritance?

The three strategies. Hibernate supports the three basic inheritance mapping strategies: table per class hierarchy. table per subclass.


2 Answers

I have used inheritance with Hibernate (and some with Django), and regreted it dearly.

The "composition over inheritance" principle is especially true for domain classes. While I agree that there is a few cases where inheritance makes sense at the model level, in most cases inheritance will give you a very static domain model, where one object will not be able to change to another class.

I also find that most developers are not comfortable with the concept of inheritance at the database level, so maintenance becomes more complicated.

And last, there are some technical problems, like proxies put in place by Hibernate that will hide the actuall class of an object. It makes "instance of" behave stangely. Of course, you might say that "instance of" is a code smell and that maybe it is another hint that composition is probably a better solution ...

like image 173
Guillaume Avatar answered Sep 20 '22 18:09

Guillaume


I like this question a lot. I've used ORM tools (Toplink, now eclipselink, Hibernate) for a while and I've always seen this as referenced in JPA documents but I've never really had a need for it. Basically my philosophy is the ORM is just there to prevent you from writing tedius code to pull out records for the database. That really is the huge timesaver and it prevents you from making stupid mistakes. Sure you can do fancy stuff with this, but why not save it for the controller (if you're following MVC) than stick it in the model?

like image 33
GBa Avatar answered Sep 22 '22 18:09

GBa