Java – hibernate 4: persistent inheritancetype Joined identifier column value

I have a simple joined document hierarchy:

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,FOREIGN KEY (id) REFERENCES SystemDocuments (id)
);

You can see that all sub tables share the same ID from the documents table. In addition, systemdocuments adds a binary_ Data column, while externaldocuments does not add new properties (also note that there are two other specific sub tables in the hierarchy, represented by 'official' and 'individual', which are irrelevant here

The following is the mapping of the above table:

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 category should be mapped to the document's identifier column value "external" When an entity is found through the entitymanager, the ID is returned correctly, actually because the ID of my test data is correctly inserted into the data block

Now I use the following code to insert new documents / files into the system through JPA and 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 checking the database, I can see that hibernate does not insert the "external" discriminator value into the identifier column of the "document" table

There were some problems in the past, see https://hibernate.onjira.com/browse/ANN-140 Recently, there is a report about hibernate 4 https://hibernate.onjira.com/browse/HHH-4358 So it is possible to work like this

Then I found it in the current hibernate 4 API documentation http://docs.jboss.org/hibernate/core/4.0/javadocs/org/hibernate/annotations/DiscriminatorOptions.html , but it doesn't work (see @ discriminatoroptions in document class)

How can hibernate 4 insert identifiers using original annotations?

Note: I do not want to map the discriminator column to a regular column

Solution

First, the problem is discriminator in inheritancetype Duplicate of joined

It looks like the persistence discriminator value in joined inheritance is not required by JPA specification This is what I received by email from members of the JPA expert group:

The problem at hand has been the problem of hibernate for some time. Please see here:

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

Declined comment:

In the end, only single_ The table policy requires a discriminator column, and joined can be implemented without The current problem with hibernate is that when persisting sub entities in joined inheritance mapped with @ discriminatorcolumn, even if the JPA specification recommends using authenticators in joined, the identifier values will still be persistent, so it will lead to inconsistent data See more in RFE:

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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>