Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1110199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:21:30+00:00 2026-05-17T02:21:30+00:00

I have an inheritance hierarchy with overlap. The system knows about People that can

  • 0

I have an inheritance hierarchy with overlap. The system knows about People that can be Clients, Providers and Agents. A person have to belong to one of these classes but can belong to two or three, i.e. one Person can be a Client and a Provider at the same time.

In the database I think that the problem is solved, one table per class (Person, Client, Provider and Agent table) and a Foreign Key from the Primary Key of the subclasses table to the Primary Key of the superclass table. (Any possible improvement will be welcome 🙂 )

The problem comes in the Java world, I don’t know the best way to map this database design to my Java POJOs. I have three possible grotty workarounds:

  • A unique class called Person, with the union of all the fields in the subclasses. This will need a discriminator field in order to know wich kind of Person is. The problem is that a Person that is not a Client but is a Provider, will have all the Client-related fields set to null.

  • A unique class called Person with all the common fields to the subclasses and three “DTO-kind” properties that holds the fields related to each subclass. This way we only have one or two fields to null instead of tens

  • One abstract class for Person and seven subclasses, one per possible combination of the three subclasses i.e. Client, Provider, Agent, ClientProvider, ClientAgent … ClientProviderAgent. :S (of course, everyone with their corresponding interfaces)

It’s a webapp. I use hibernate + spring and GWT for the UI

The question is: which is the best way to solve this problem?

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T02:21:31+00:00Added an answer on May 17, 2026 at 2:21 am

    IMO inheritance is not the best way to model this, I would try composition instead.

    You could have a Person class and several Role classes (implementing a common interface, or being members of a Role enum, depending on the context), with each person having one or more Roles attached.

    This way you can add new role types easily, and dynamically attach/detach roles to/from a person. (You can also have persons without a role, should the need arise.)

    Rough example:

    interface Role {
      ...
    }
    
    final class Client implements Role {
      ...
    }
    
    final class Provider implements Role {
      ...
    }
    
    final class Agent implements Role {
      ...
    }
    
    class Person {
      List<Role> roles;
      public void addRole(Role role) { ... }
      public void removeRole(Role role) { ... }
      public Role getRoleOfType(Class<? extends Role> roleType) { ... }
    }
    

    Update: enum based example

    This is applicable if the role objects have no state, thus you attach the same role instance(s) to every person.

    enum Role {
      CLIENT,
      PROVIDER,
      AGENT;
      // possible members, constructor etc.
    }
    

    The Person class is almost the same as above, except that

    • I use an EnumSet instead of a List, since this is tailored specifically for enums,
    • getRoleOfType() makes no sense here, so I replaced it with hasRole().

      class Person {
        Set<Role> roles = new EnumSet<Role>();
        public void addRole(Role role) { ... }
        public void removeRole(Role role) { ... }
        public boolean hasRole(Role role) { ... }
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a legacy database that uses a table per class hierarchy inheritance strategy
I have a situation where I am mapping classes that have an inheritance hierarchy.
Given a simple inheritance hierarchy: Person -> Student, Teacher, Staff Say I have a
For example, I have some class hierarchy (possibly, with all kinds of inheritance -
I know with OO Perl I can have objects and inheritance, but are interfaces
i have been reading about multiple inheritance What is the exact problem with multiple
Suppose I have a small inheritance hierarchy of Animals: public interface IAnimal { string
If you declare an inheritance hierarchy where both the parent and child class have
I have the following jpa entity inheritance hierarchy: abstract Account ChildminderAccount extends Account ParentAccount
I have the following inheritance hierarchy: public interface IRepository<T> : IDisposable { void Add(T

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.