2

Right now I am using a table per subclass approach to model my data. A simplification of my hierarchy is:

abstract class Abstract {
    /* common data stored in abstract */
}

class ConcreteTypeA1 extends Abstract {
    /* extra data stored in concrete_type_a_1 */
}

class ConcreteTypeA2 extends Abstract {
    /* extra data stored in concrete_type_a_2 */
}

class ConcreteTypeB extends Abstract {
    /* extra data stored in concrete_type_b */
}

So it does three outer joins where I grab instances of type Abstract (in reality it is twelve). What I realized yesterday is that ConcreteTypeA1 and ConcreteTypeA2 really have the same extra data, they just behave differently, so what I would like to do is reduce the number of joins by stuffing these two classes into one table and using a discriminator column. How / can I accomplish this?

class Abstract {
    /* common data stored in abstract */
}

abstract class ConcreteTypeA extends Abstract {
    /* extra data stored in abstract_type_a */
}

class ConcreteTypeA1 extends ConcreteTypeA {
    /* just behavior, no extra data, uses data in abstract_type_a */
}

class ConcreteTypeA2 extends ConcreteTypeA {
    /* just behavior, no extra data, uses data in abstract_type_a */
}

class ConcreteTypeB extends Abstract {
    /* extra data stored in concrete_type_b */
}
Ransom Briggs
  • 3,025
  • 3
  • 32
  • 46
  • 2
    See this answer: http://stackoverflow.com/questions/3915026/how-to-mix-inheritance-strategies-with-jpa-annotations-and-hibernate/3916998#3916998 – Vincent Ramdhanie Jun 01 '11 at 14:33

2 Answers2

1

use this on Parent class

@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
    name="type",
    discriminatorType=DiscriminatorType.STRING)

and on concrete classes use

@DiscriminatorValue("TypeA")
Manoj
  • 5,542
  • 9
  • 54
  • 80
  • This answer is missing the important addition of using @SecondaryTable(table = "abstract_type_a") and @Column(table = "abstract_type_a") in ConcreteTypeA that I found in Vincents link. – Ransom Briggs Jun 01 '11 at 15:47
  • @RansomBriggs This is showing the "table per class hierarchy" approach; the other one is using a mixed approach with a discriminator column plus auxiliary tables. – augurar Nov 03 '14 at 23:43
1

Used answer from this question as per Vincents' advice.

How to mix inheritance strategies with JPA annotations and Hibernate?

Community
  • 1
  • 1
Ransom Briggs
  • 3,025
  • 3
  • 32
  • 46