Note that there are some explanatory texts on larger screens.

plurals
  1. POCan I remove the discriminator column in a Hibernate single table inheritance?
    primarykey
    data
    text
    <p>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:</p> <pre><code>@Entity @Inheritance(strategy = InheritanceType.SINGLE_TABLE) @Table(name = "client") public abstract class Client extends AbstractPersistable&lt;Long&gt; { // ... } </code></pre> <p>application A:</p> <pre><code>@Entity public class ClientSimple extends Client { private String name; // getter, setter } </code></pre> <p>application B:</p> <pre><code>@Entity public class ClientAdvanced extends Client { private String description; // getter, setter } </code></pre> <p>Now a DAO can work with <code>Client</code> 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:</p> <p>application A:</p> <pre><code>Client client = new ClientSimple(); clientDao.save(client); </code></pre> <p>application B:</p> <pre><code>Client client = new ClientAdvanced(); clientDao.save(client); </code></pre> <p>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...</p> <p>Thanks!</p> <hr> <p><strong>EDIT</strong></p> <p>Important to note: <code>@MappedSuperclass</code> won't work. We're using <strong><em>QueryDSL</em></strong> 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 <code>@Entity</code>. </p> <p>This is neccessairy because we want to query against the abstract class <code>Client</code> while in truth querying <code>ClientSimple</code> in application A and <code>ClientAdvanced</code> in application B:</p> <p>So in any application this will work:</p> <pre><code>query.where(QClient.client.name.equals("something"); </code></pre> <p>and in application B this will work: </p> <pre><code>query.where(QClientSimple.client.description.equals("something else"); </code></pre> <hr> <p><strong>EDIT2 - boil down</strong></p> <p>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 <code>Client</code> will always be <code>ClientSimple</code> in one application and <code>ClientAdvanced</code> in the other so that I don't have to store that information in the database? </p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload