Search code examples
hibernatedatabase-designinheritancejpaspring-roo

Database modeling issue with inheritance and JPA2/Hibernate


I use JPA/Hibernate, MySQL and Spring Roo and I am in the process of modeling an application that will have two different types of members:

  • Male members
  • Female members

As of now, I have one table/entity for both male and female members.

However, I am now realizing that some fields/attributes will be different according to whether the member is a male or female.

For instance, I can have an "beard style" attribute for men that will contains the following possible values: "trimmed, long, shaven".

I thought of several options to represent this in my domain model:

  1. Go for some sort of JPA inheritance. However, I am not sure which strategy to choose and which strategy is supported by Hibernate.
  2. Have two fields maleSpecificInfo and femaleSpecificInfo in my Member table/entity. If the member is a female, the maleSpecificInfo will be null and vice-versa.

Can anyone please provide advice as to which of my options, if any, is acceptable. Or provide a better alternative?


Solution

  • I dislike the second approach. A male person object might contain a relation to FemaleSpecificInfo, that smells badly to me. I don't think that "composition over inheritance" implementation is right.

    I'd try Hibernate inheritance. You should check the strategies . If male and female share most of the data I'd use Table per class hierarchy.