Hibernate Inheritance and Discriminators

 In Technical

An alternative way to declare discriminators using @DiscriminatorFormula. I’ve been using Hibernate for going on 5 years now and there are still more things to learn!

I was looking into the “single table per hierarchy” method of mapping inheritance with Hibernate and found out something new about how to declare the discriminator.

With a standard discriminator, you need a separate column in this method to tell Hibernate what type of object can be found in this row. In some cases, it’s easy to have this extra column; but in some cases you don’t have the control you would like over the structure of the data in the database.

If you can’t have a separate discriminator column, you can use the @DiscriminatorFormula annotation to pass in a sql fragment. Hibernate will then use this to determine the class of a particular row of data stored with this method.

See the Hibernate Annotations reference on inheritance for more information.

Start typing and press Enter to search