Search code examples
javahibernateinheritancesingle-table-inheritancequerydsl

Can I remove the discriminator column in a Hibernate single table inheritance?


We use single table inheritance for every table in our application. This allows different instances of the same application stack to work with the same DAOs while their entities might differ slightly potentially containing information unique to that instance. An abstract class defines the basic table structure and an extension defines additional columns, if needed by that instance:

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Table(name = "client")
public abstract class Client extends AbstractPersistable<Long> {
    // ...
}

application A:

@Entity
public class ClientSimple extends Client {
    private String name;
    // getter, setter
}

application B:

@Entity
public class ClientAdvanced extends Client {
    private String description;
    // getter, setter
}

Now a DAO can work with Client objects for application A and B but application B can define additional information for its client object that may be read by a manager method unique to application B:

application A:

Client client = new ClientSimple();
clientDao.save(client);

application B:

Client client = new ClientAdvanced();
clientDao.save(client);

Unfortunately this means there is a DTYPE column in every table (or any other name that I might choose). Is there any way to get rid of this? We don't need it and it's using up DB space...

Thanks!


EDIT

Important to note: @MappedSuperclass won't work. We're using QueryDSL as our HQL abstraction layer. This requires automatically generated Query Type classes for type save querying. These however will only be generated correctly if the abstract class is annotated with @Entity.

This is neccessairy because we want to query against the abstract class Client while in truth querying ClientSimple in application A and ClientAdvanced in application B:

So in any application this will work:

query.where(QClient.client.name.equals("something");

and in application B this will work:

query.where(QClientSimple.client.description.equals("something else");

EDIT2 - boil down

It seems to boil down to this: Can I configure hibernate at deploy time to set the discriminator type for an inhertited entity to a fixed value. So going with my example a Client will always be ClientSimple in one application and ClientAdvanced in the other so that I don't have to store that information in the database?

Like I said: Each application will be an instance of the base application stack. Each application might define additional columns for their local database but ALL objects will be of the same type for that instance so we guarantee that the discriminator is always the same making it redundant in the database and a use case for hibernate configuration.


Solution

  • If you never need to use both ClientSimple and ClientAdvanced in the same application you can declare Client as @MappedSuperclass rather than @Entity.