Skip to main content

Inheritance and Custom Mapping

  • Chapter
  • First Online:
Hibernate Recipes

Abstract

Inheritance, polymorphic associations, and polymorphic queries are supported by entities. The JPA specification says that both concrete and abstract classes can be entities and can be mapped with the Entity annotation. Non-entity classes and entity classes can extend each other. Hibernate provides the MappedSuperclass annotation (@MappedSuperclass) to enable inheritance mapping and to let you map abstract or concrete entity subclasses. The mapped superclass doesn't necessarily have its own separate table.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 29.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD 39.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Notes

  1. 1.

    In addition to data integrity being compromised (thanks to the non-null requirement), changes to any members of the hierarchy involve changing the data model for all members of the hierarchy.

  2. 2.

    The performance advantage of a single table per hierarchy is based on the fact that a single select can suffice to return many different subclass instances instead of Hibernate having to run many different queries to collect different instance types.

  3. 3.

    In case you’re wondering, this model would not work well in the real world. That’s okay; we’re just using a hypothetical. A good exercise is to actually model a class hierarchy that would consider floppy discs of various sizes and capacities, and CDs, DVDs, Blu-Ray, and other such discs, along with their content types.

  4. 4.

    A “database row” is also known as a “tuple,” which is the term future references will use. Feel the tuple. Love the tuple. Be the tuple. Or don’t. That would be a little creepy and probably call your concept of identity into question.

  5. 5.

    Note that we will modify this configuration to add any additional information the rest of this chapter might need.

  6. 6.

    We can’t use a single query to pull back all polymorphic references for some hierarchical strategies because they’re not necessarily represented in a simple known set of tables; a single query for all subclasses could theoretically query many, many, many more tables than you’d want it to.

  7. 7.

    Plus, all blank discs—the superclass’ type—would have to have a director, which would make Hollywood very happy, but blank discs very expensive and make consumers very, very upset.

  8. 8.

    Contrast this with the single-table approach. A VideoDisc doesn’t have any of the AudioDisc attributes, but the database has to have the AudioDisc data as part of every object descended from Disc. In the joined approach, the primary key—the join field—is duplicated, but nothing else is.

  9. 9.

    We can’t use the IDENTITY strategy with primary keys any longer because each table would have its own identity. Although the database could have a Disc3 with identity 2 and a VideoDisc3 with identity 2, the object hierarchy couldn’t logically have the two objects with the same identifier.

  10. 10.

    Well, let’s say that it isn’t possible given normal human amounts of effort. You probably could find a way around the polymorphism issue, but it’s probably not worth it.

  11. 11.

    See http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html/ch05.html#mapping-types-basictypes for a comprehensive list of the Hibernate internal type mappings.

  12. 12.

    Lombok uses attribute order in the source file to work out where to place values in parameter lists. If someone were to invert the latitude and longitude, the values would be switched with no warning. This would be ungood. Therefore, we’re explicitly including the constructor.

  13. 13.

    If this looks too long to be worthwhile, remember that embeddable objects work, too; this is really a way of exposing the machinery of embedded objects.

Author information

Authors and Affiliations

Authors

Rights and permissions

Reprints and permissions

Copyright information

© 2015 Joseph Ottinger

About this chapter

Cite this chapter

Ottinger, J., Guruzu, S., Mak, G. (2015). Inheritance and Custom Mapping. In: Hibernate Recipes. Apress, Berkeley, CA. https://doi.org/10.1007/978-1-4842-0127-5_4

Download citation

Publish with us

Policies and ethics