Improving the JPA polymorphic association construction

I have the following JPA entity hierarchy :

class diagram for jpa objects

When logging into the application, the user has an instance UserAccount

in the session; then, depending on the specific type of organization associated with this UserAccount

( Admin

, Business

or Family

), another panel / screen is displayed to the user or, for example. if the user is Business

, then the dashboard for that business is displayed.

My concern with this design is that I have to check instanceof

every time the user logs in so that I know what type of panel to display. I could also avoid validation instanceof

by having a property in UserAccount

such as organizationType

(which takes one of three values), but then there will be redundant information.

Is there a way to improve my design? If so, how?

+3


source to share


1 answer


Be greedy and get both, no redundancy.

Depending on your inheritance strategy, you may already have the information organizationType

and publish it for free.

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "DTYPE")
public abstract class AbstractOrganization implements Serializable
{
    @Id
    protected Long id;

    @Column(name = "DTYPE", insertable = false, updatable = false)
    protected String organizationType;

    ...

}

      



the same applies to strategy JOINED

.

Don't implement the method setOrganizationType

.

Since TABLE_PER_CLASS

there is a discriminator for modeling a hierarchy with tables (except for a strategy ), there is no redundancy and the JPA provider will handle this attribute for you.

0


source







All Articles