Questions tagged [single-table-inheritance]

Single table inheritance is the simplest of several ways to design SQL tables that reflect a class/subclass or generalization/specialization relationship.

Single table inheritance is the simplest of several ways to design SQL tables that reflect a class/subclass or generalization/specialization relationship.

Inheritance is a central concept in object modeling, and is dealt with in class/subclass models. It is well understood by people who build object oriented systems. The parallel concept in ER (Entity-Relationship) modeling is called generalization/specialization. Unfortunately, many introductions to ER modeling do not present generalization/specialization, leaving the beginner to reinvent the concept on their own.

SQL, as such, has no mechanism for implementing inheritance. There are several design techniques for mimicking the effects of inheritance in SQL tables. The simplest of these techniques is called single table inheritance.

In single table inheritance, a single table is used to retain data that pertains to either the superclass or any of its subclasses. Each attribute will have its own column, and each instance will have its own row.

The result of this design is that all data about any member of the class can be obtained without doing any joins. If the intersection of a given column and a given row is not applicable, it is left as an SQL NULL.

SQL NULLS do result in slower retrieval of the rows that contain them, but this is generally offset by not having to do joins. SQL NULLS do increase the amount of space needed to store rows that contain them, but this is generally a secondary consideration.

Where NULLS can be problematic is when they appear in Boolean comparisons like equality tests in WHERE clauses. In SQL a boolean test can result in three possibilities: TRUE, FALSE, or UNKNOWN. If either side or both sides in a comparison are NULL, the result is UNKNOWN. If a test for equality yields UNKNOWN, the same test for inequality will also yield UNKNOWN. This can be baffling to people who are used to two valued logic. The use of nullable columns in WHERE clauses has to be considered very carefully, in order to avoid unexpected results.

Another issue is that it can be difficult to tell which subclass a given row belongs to. For this reason, a separate column, often called EntityType (e.g. VehicleType), is used to indicate subclass membership explicitly.

In complex situations, there are two alternatives to single table inheritance. One is called class table inheritance, which has its own tag: . another is called concrete table inheritance.

616 questions
0
votes
1 answer

hibernate + single table inheritance + discriminatorColumn --> DiscriminatorType cannot be resolved to a variable

I am learning hibernate inheritance and would like to change the discrimination column from Stringt o INTEGER so I can reduce the size of my database index. I have something like this: @Entity(name = "events") @Inheritance(strategy =…
masber
  • 2,875
  • 7
  • 29
  • 49
0
votes
1 answer

How to get a the field value from another class that has inherit from sales.order?

I got this situation, where I need to get the initial price from class B because the computation is inherited to the 'sale.order' which I cannot put in model A. How to do this since every time I move the calculations to class model_A, it will have…
William
  • 89
  • 1
  • 14
0
votes
0 answers

Spring JPA SINGLE_TABLE inheritance not working - Table for sub class does not exist

I am trying to utilise Spring JPA inheritance for my entities. Below are the code snippets of the annotations that I use for the classes. The application is based on Spring Boot v2.2.2.RELEASE. @Entity @Inheritance(strategy =…
0
votes
0 answers

Undefined Method Error - Single Table Inheritance - Routing error

I am creating a Single Table Inheritance model to build a comment thread and I am getting a confusing routing error I have set up my Model class Question < ApplicationRecord has_many :answers, class_name: 'Questions::Answer' end class Answer <…
0
votes
1 answer

Loop over related model's children in Django template

I have a model for a company. Then I have a base model for company posts. It contains common posts attributes. An attribute is the company that publishes the posts. It refers to the Company model with a ForeignKey. Finally I have a child model…
0
votes
2 answers

Examples and advices on combining a Polymorphic Association and a Single Table Inheritance

I am using Ruby on Rails 3 and I would like to know a real world example (or more) and some advice on when I should\can\must use the combination of a Polymorphic Association and a Single Table Inheritance as implemented here at the Polymorphic…
0
votes
0 answers

Doctrine: Allow extending STI entities from within 3rd party code

Background I've got an entity A with single table inheritance set up and class Foo extending it like so: /** * @ORM\Entity() * @ORM\InheritanceType("SINGLE_TABLE") * @ORM\DiscriminatorColumn(name="type", type="string") * @ORM\DiscriminatorMap( …
mvo
  • 1,138
  • 10
  • 18
0
votes
1 answer

Where to write associations in Single Table Inheritance in rails?

I have a base class called User and it has 2 subclasses named Manager and Creator. I have implemented Single Table Inheritance for User, Manager and Creator models. There are 2 other models named Project and Bug. class User <…
0
votes
0 answers

How to access multiple entities data from a single table type @inheritance table using Thymeleaf in spring boot project?

link to my project Where I am using single table type inheritance and then using thymleaf , I am trying to access the table's content and trying to put them into a single html page using a single tag.
0
votes
1 answer

get by id in hibernate returns null when id is defined in the parent entity

I have 3 entities, all mapped to the same base table, like this: @Inheritance(strategy = InheritanceType.SINGLE_TABLE) @DiscriminatorColumn( discriminatorType = DiscriminatorType.STRING, name = "disc_type" ) public class Parent { @Id …
SockworkOrange
  • 355
  • 4
  • 14
0
votes
2 answers

Hibernate - One to one relation single table inheritance

I have 3 entities BaseFoo, FooAbc, FooAbcDetail. FooAbc extends base entity BaseFoo. I'm trying to do one to one relation between FooAbc and FooAbcDetail. @Data @EqualsAndHashCode(callSuper = true) @Entity @Table(name =…
mnesimi
  • 65
  • 1
  • 9
0
votes
1 answer

Rails single table inheritance with reference different columns in different models

How can I have 2 different references columns in 2 models which inherit from a common class using single table inheritance? I have found this Single Table Inheritance (STI) column associations answer but as it is 10 years ago is there a better…
0
votes
0 answers

Inheritance Single Table with springboot

i'm working with springboot , inheritance using single table, i have problem with the add function , how to insert the discriminator column value automatically , ps : i'm not working with entityManager. thank you :) Here is th code of 3 entities…
Gharbi
  • 125
  • 3
  • 11
0
votes
0 answers

How to set 'create' in _form.html.erb and .controller for single inheritance with self nested routes

I am new at rails and I am trying to have a subpost inherited from a post. A post belongs_to user and has_many subposts. I am unsure of how to create the forms and controller that will allow the post become of type subpost. I've tried looking at…
XXX
  • 47
  • 9
0
votes
0 answers

How can i add oneToMany mapping with inheritence SINGLE_TABLE

I'm using inheritence with JPA and HIBERNATE so i have these entities : @Entity @Inheritance( strategy = InheritanceType.SINGLE_TABLE ) @DiscriminatorColumn( name = "entityType", discriminatorType = DiscriminatorType.STRING ) public…
Sidaoui Majdi
  • 399
  • 7
  • 26