5 comments on “Mapping Class Hierarchy in Hibernate

  1. Pingback: What is Implicit about Implicit Polymorphism in Hibernate? | Notes about developing web stuff

  2. In terms of data integrity and from a modelling perspective, I always wonder why anyone would want to let a Java-to-SQL mapping drive the database model.

    To me, the table-per-subclass model seems to be the only reasonable one from a relational perspective. The table-per-class-hierarchy model leads to a waste of space and to potential bogus data (e.g. a plane with wagons), whereas the table-per-concrete-class model will make it difficult to guarantee constraint integrity if other tools than Hibernate operate on the database (e.g. other applications, migration scripts, etc.).

    Also, I wonder if @Embedded and @Embeddable wouldn’t be the better choice in the Java model, allowing for choosing composition over inheritance. E.g. the three vehicle types have-a capacity, instead of them being-a vehicle.

  3. The advantage with Hibernate type mapping tools is that it saves you a LOT of labor from writing the CRUD queries, and introduce bugs in the process. Hibernate does the job well, besides you can leverage the caching and lazy loading advantages for your application.
    But if you have other backdoor entries into your database, then you can’t hold Hibernate responsible for them messing the data integrity. Yes, table-per-subclass is robust, but table-per-class hierarchy is simpler and each has its pros and cons.

    • No, of course, Hibernate is not responsible for data integrity issues introduced by bad overall architectures, and it’s good to offer several options.

      I’m just thinking that from a SQL perspective, two out of three options are “risky” in the sense of not being very SQLesque. But I don’t have enough experience on that matter, so I might be missing some of the pros of the non-table-per-subclass approaches…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s