Questions tagged [discriminator]

A discriminator is a column used in object-relational mapping problems to describe inheritance. It is often, but not exclusively, associated with the single table per class hierarchy approach.

Inheritance hierarchies arise naturally in object-oriented design and programming; however when it comes to persisting the object a relational database management system (RDBMS) is normally used. A discriminator is a column used to describe inheritance. It is often, but not exclusively, associated with the single table per class hierarchy.

Consider the following example that consists of students and teachers. One can create a people table that contains the common information about the students, say first and last name. The categories table will contain two entries, student and teacher and the column fk_category on the people table functions as a discriminator.

CREATE TABLE categories
(
  id serial NOT NULL,
  category character varying(128) NOT NULL,
  CONSTRAINT people_pk PRIMARY KEY (id)
)


CREATE TABLE people
(
  id bigserial NOT NULL,
  fk_category INT NOT NULL,
  first_name character varying(128) NOT NULL,
  last_name character varying(128) NOT NULL,

  -- other columns common to teachers and students

  CONSTRAINT people_pk PRIMARY KEY (id),
  CONSTRAINT people_fk FOREIGN KEY (fk_category)
  REFERENCES categories (id) MATCH SIMPLE
)


CREATE TABLE students
(
  fk_person bigint NOT NULL,

   -- other columns specific to students

  CONSTRAINT students_pk PRIMARY KEY (fk_person),
  CONSTRAINT students_fk FOREIGN KEY (fk_person)
  REFERENCES people (id) MATCH SIMPLE
)

CREATE TABLE teachers
(
  fk_person bigint NOT NULL,

   -- other columns specific to teachers

  CONSTRAINT teachers_pk PRIMARY KEY (fk_person),
  CONSTRAINT teachers_fk FOREIGN KEY (fk_person)
  REFERENCES people (id) MATCH SIMPLE
)

More Information:

  1. Wikipedia on Object-relational mapping
  2. The first chapters of Java Persistence with Hibernate provide an excellent overview of the problem and potential solutions.
224 questions
46
votes
6 answers

Doctrine 2 - How to use discriminator column in where clause

I was used discriminator column in where clause like this: //f = root entity $qb = $this->createQueryBuilder('f'); $qb->add('where', 'f.format = \'image\' OR f.format = \'text\''); I've got an error: "Message: [Semantical Error] line 0, col 73 near…
Can Aydoğan
  • 1,040
  • 2
  • 10
  • 23
13
votes
3 answers

DiscriminatorColumn as part of primary key / id

Situation I have an Entity with a DiscriminatorColumn, configured for single table inheritance: @Entity @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name="TYPE") public class…
Spycho
  • 7,698
  • 3
  • 34
  • 55
11
votes
1 answer

Hibernate 4: persisting InheritanceType.JOINED discriminator column values

I have a simple JOINED hierarchy of documents: CREATE TABLE Documents ( id INTEGER NOT NULL, discriminator ENUM('official','individual','external') NOT NULL, file_name VARCHAR(200) NOT NULL, PRIMARY KEY (id) ); CREATE SystemDocuments ( id…
Kawu
  • 13,647
  • 34
  • 123
  • 195
10
votes
3 answers

How to access discriminator column in JPA

I have DisseminationArea as subcalss for Feature with the following code: @Entity @Table(name = "features") @Inheritance(strategy = InheritanceType.JOINED) @DiscriminatorColumn(name = "subtype_id", discriminatorType =…
Dims
  • 47,675
  • 117
  • 331
  • 600
9
votes
3 answers

How to select discriminator column in doctrine 2

I need some help when select only discriminator column from doctrine 2 when run the DQL below SELECT p.type FROM AppBundle\Entity\Product p type is discriminator column in entity AppBundle\Entity\Product @ORM\DiscriminatorColumn(name="type",…
Chin Lee
  • 133
  • 1
  • 1
  • 8
9
votes
1 answer

DISCRIMINATOR based Multi-Tenancy with Spring Data JPA+Hibernate

I want to implement DISCRIMINATOR based Multi-Tenancy solution for Shared Schema Based Multi-Tenancy Model-common database schema for all tenants. Technology stack Hibernate 3 Global filter (I can't use EclipseLink) Spring Data JPA - I want this…
Ketan
  • 2,612
  • 5
  • 31
  • 44
8
votes
2 answers

How to get a list of available Mongoose Discriminators?

Given a situation where you have a User Scheme that you use to create a base model called User. And then for user roles, you use mongoose discriminators to create inherited models called Admin, Employee and Client. Is there a way to programmatically…
8
votes
3 answers

Hibernate creating wrong entity subtype in relationship

I have a strange issue where hibernate does not create the expected entity type in a many to one relataionship. We have the following entities with subclass hierarchy (simplified): @Entity @Table(name = "A") @Inheritance(strategy =…
Gandalf
  • 2,350
  • 20
  • 28
7
votes
0 answers

mongoose: Populating a discriminated subdocument

I want to populate the fields of a subdocument, which is a discriminated element of a common Schema (Notificationable discriminated into Message or FriendRequest). This question is quite similar to this one: mongoosejs: populating an array of…
7
votes
2 answers

Map subclass as its extended parent

I have created the following abstract class, which use single table inheritance and maps subclasses on the DiscriminatorColumn model. /** * @Entity * @Table(name="entity") * @InheritanceType("SINGLE_TABLE") * @DiscriminatorColumn(name="model",…
Bas Peeters
  • 3,269
  • 4
  • 33
  • 49
7
votes
0 answers

Multi-Level Inheritance in Joined Inheritance strategy using discriminator for each parent in hierarchy

Consider my classes below Product (Abstract Root class) @Entity @Table(name="PRODUCT") @Inheritance(strategy=InheritanceType.JOINED) @DiscriminatorColumn(name="PRODUCT_TYPE") public abstract class Product { @Id @GeneratedValue …
Jeff
  • 519
  • 1
  • 10
  • 18
6
votes
1 answer

Discriminator issue in inheritance mapping of Grails

I have 4 classes A, B, B1, B2 with inheritance mapping described as below: A (mapped to table A) is the top-most parent class and its inheritance mapping strategy is tablePerHierarchy=false (means each of its sub-classes is mapped to a single…
Đinh Hồng Châu
  • 5,300
  • 15
  • 53
  • 90
6
votes
3 answers

Regarding Discriminator in EF Core, is there a way to set the base class with all values?

I want that when retrieving the base class I should get all rows in the table disregarding the discriminator. I only want to use the discriminator when retrieving one of the derived classes. How can I accomplish that? The first thing that I tried…
A Y B
  • 111
  • 1
  • 8
6
votes
2 answers

NHibernate Discriminated Subclasses of a Joined-Subclass

Here's my heirarchy: class abstract Entity { /*members*/ } // mapped to entity table class abstract User : Entity { /*members*/ } // mapped to user table class Employee : User { /*no members*/ } // no table, discriminator = "E" class Contractor :…
Travis Heseman
  • 11,359
  • 8
  • 37
  • 46
6
votes
1 answer

How can I use Fluent NHibernate to discriminate on a column of a parent relationship

I have this entity relationship. public abstract class UserGroup { public virtual int UserGroupId { get; set; } public virtual int GroupType { get; set; } public virtual string GroupName { get; set; } public virtual…
1
2 3
14 15