gpt4 book ai didi

java - Hibernate 4 : persisting InheritanceType. JOINED 鉴别器列值

转载 作者:搜寻专家 更新时间:2023-10-30 21:45:29 24 4
gpt4 key购买 nike

我有一个简单的 JOINED 文档层次结构:

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)
);

如您所见,所有子表所做的是共享 Documents 表中的相同 ID。除此之外,SystemDocuments 添加了一个 binary_data 列,而 ExternalDocuments 没有添加任何新属性。 (另请注意,层次结构中还有另外两个具体子表,分别用 'official''individual' 表示,与此处无关。)

以下是上述表格的映射:

文档.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
{
...
}

后一类应该映射到文档的鉴别器列值 'external'。当通过 EntityManager.find 查找实体时,鉴别器被正确返回,实际上是因为我的测试数据的鉴别器已正确插入到数据库中。

现在我使用以下代码通过 JPA 和文件 uploader 将新文档/文件插入到系统中:

...

UploadedFile uf = event.getUploadedFile();

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

docService.create(detachedExternalDocument);

然而,当检查数据库时,我可以看到 Hibernate 确实'external' 鉴别器值插入到Documents 表的鉴别器列。

过去有过这方面的问题,请参阅 https://hibernate.onjira.com/browse/ANN-140 和最近的 Hibernate 4 https://hibernate.onjira.com/browse/HHH-4358,所以它很可能应该以这种方式工作。

然后我在当前的 Hibernate 4 API 文档中找到了 http://docs.jboss.org/hibernate/core/4.0/javadocs/org/hibernate/annotations/DiscriminatorOptions.html,但它不起作用(请参阅文档类中的@DiscriminatorOptions)。

如何让 Hibernate 4 插入鉴别器使用原始注释

注意:我不想将鉴别器列映射为常规列。

最佳答案

首先,这个问题与Discriminator in InheritanceType.JOINED重复。 .

JPA 规范不需要要求在 JOINED 继承中保留鉴别器值。以下是我通过电子邮件从 JPA 专家组成员那里收到的内容:

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.

手头的问题一直是 Hibernate 的问题,请看这里:

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

拒绝评论:

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.

最后只有 SINGLE_TABLE 策略需要一个鉴别器列,JOINED 可以在没有的情况下实现。 Hibernate 当前的问题是,在使用 @DiscriminatorColumn 映射的 JOINED 继承中持久保存子实体时,它会导致数据不一致,即使 JPA 规范建议在 JOINED 中使用鉴别器时持久保存鉴别器值。在 RFE 中查看更多信息:

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

关于java - Hibernate 4 : persisting InheritanceType. JOINED 鉴别器列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7988756/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com