Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6952321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:20:02+00:00 2026-05-27T14:20:02+00:00

I have a simple JOINED hierarchy of documents: CREATE TABLE Documents ( id INTEGER

  • 0

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 INTEGER NOT NULL,
  binary_data BLOB NOT NULL,
  PRIMARY KEY (id),
  FOREIGN KEY (id) REFERENCES Documents (id)
);

CREATE ExternalDocuments
(
  id INTEGER NOT NULL,
  PRIMARY KEY (id),
  FOREIGN KEY (id) REFERENCES SystemDocuments (id)
);

As you can see all the sub tables do is share the same ID from the Documents table. Other than that SystemDocuments adds a binary_data column and ExternalDocuments adds no new properties. (Also note there are two other concrete sub tables in the hierarchy denoted by 'official' and 'individual' which are of no relevance here.)

Here are the mappings for the above tables:

Document.java:

@Entity
@Table(name = "Documents")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "discriminator", discriminatorType = DiscriminatorType.STRING)
//@DiscriminatorOptions(force = true) // <-- Hibernate 4-specific annotation not inserting discriminator values
public abstract class Document implements Serializable
{
    @Id
    @Column
    protected Integer id;

    @Column(name = "file_name")
    protected String fileName;

    ...
}

SystemDocument.java:

@Entity
@Table(name = "SystemDocuments")
public abstract class SystemDocument extends Document
{
    @Lob
    @Column(name = "binary_data")
    protected byte[] binaryData;

    ...
}

ExternalDocument.java:

@Entity
@Table(name = "ExternalDocuments")
@DiscriminatorValue(value = "external")
public class ExternalDocument extends SystemDocument
{
    ...
}

The latter class is supposed to be mapped to the Documents’ discriminator column value 'external'. When finding entities via EntityManager.find the discriminators are returned correctly, well actually because the discriminators of my test data have been INSERTed into the DB correctly.

Now I use the following code to insert new documents/files into the system via JPA and a file uploader:

...

UploadedFile uf = event.getUploadedFile();

// set ID, file name, and binary data
ExternalDocument detachedExternalDocument =
    new ExternalDocument(1234567, uf.getName(), uf.getData());

docService.create(detachedExternalDocument);

When inspecting the DB however I can see that Hibernate does not insert the 'external' discriminator value into the Documents table’s discriminator column.

There have been issues about this in the past, see https://hibernate.onjira.com/browse/ANN-140 and more recently for Hibernate 4 https://hibernate.onjira.com/browse/HHH-4358, so chances are it’s supposed to work that way.

I then found http://docs.jboss.org/hibernate/core/4.0/javadocs/org/hibernate/annotations/DiscriminatorOptions.html in the current Hibernate 4 API Docs, but it doesn’t work (see @DiscriminatorOptions at the Document class).

How can I get Hibernate 4 to insert the discriminators using raw annotations?

Note: I don’t want to map the discriminator column as a regular column.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:20:02+00:00Added an answer on May 27, 2026 at 2:20 pm

    First of all, this question is a duplicate of Discriminator in InheritanceType.JOINED.

    It seems like persisting discriminator values in JOINED inheritance is not required by the JPA spec. Here’s what I’ve received from a member of the JPA expert group via email:

    The spec does not require an implementation to use discriminator columns to implement JOINED inheritance, however, the assumption is that if @DiscriminatorColumn is specified then it would be used, i.e. the values would be written out. We do not explicitly state that if a @DiscriminatorColumn is specified in the code it must be used, just like we don’t explicitly state that if a @Column or @JoinColumn is specified the values must be stored in the table, but there is only so much that we can or should specify. At the lowest level, certain laws of physics and reason are just assumed.

    The problem at hand has been an issue with Hibernate for quite a while, see here:

    https://hibernate.atlassian.net/browse/ANN-140

    Rejection comment:

    EJB3 does NOT require use of discriminators with JOINED mapping strategies. It is allowed for inferior implementations of the JOINED mapping strategy which require a discriminator. Hibernate does not need a discriminator because Hibernate is better than these other inferior implementations.

    In the end only SINGLE_TABLE strategy requires a discriminator column, JOINED can be implemented without. The problem with Hibernate currently is that it causes inconsistent data when persisting sub entities in a JOINED inheritance mapped with @DiscriminatorColumn, even though the JPA spec recommends to persist discriminator values if a discriminator is used with JOINED. See more in the RFE here:

    https://hibernate.atlassian.net/browse/HHH-6911

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple stored proc with two queries joined with a union: select
Have simple Order - Item app. No cart. Need help with joined scopes for
I have a SQL table that will be be joined with two subtables. One
This may seem simple, but somehow it isn't. I have a table of historical
I have simple regex \.*\ for me its says select everything between and ,
I have simple win service, that executes few tasks periodically. How should I pass
i have simple regular expression: ^123$ Matches are for example 123 1234 etc. How
I have simple issue setting a two-way databinding of a checkbox in Silverlight 3.0.
I have simple form. <form target=_blank action=somescript.php method=Post id=simpleForm> <input type=hidden name=url value=http://...> <input
I have simple SSIS package which reads data from flat file and insert into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.