Hibernate 忽略鉴别器列 - 始终使用“dtype”

问题描述 投票:0回答:2

我的 SINGLE_TAB 继承 Hibernate 配置中有一个奇怪的情况,其中 @DiscriminatorColumn 似乎被忽略,并且查询始终默认返回“dtype”列。这就像我根本没有包含注释时看到的行为(默认列名称为“dtype”)。

基础实体:

@Entity
@Table(name = "post")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(columnDefinition = "post_type", discriminatorType = DiscriminatorType.STRING)
public class Post {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @Column(name = "title")
    private String title;

    @Column(name = "body")
    private String body;

    @NotNull
    @Column(name = "post_type", insertable = false, updatable = false)
    private String postType;

    // other simple columns



    // ommit getters/setters + hashcode etc
}

子类实体:

@Entity
@DiscriminatorValue(value = "EVENT")
public class Event extends Post {

    // ommitted basic methods, no extra config

}

我还需要访问每个对象中的鉴别器值本身(postType 字段)。即使我删除它,我仍然有相同的行为,所以它似乎不是原因。

当我尝试通过 JPA 存储库对子类进行查询时:

public interface EventRepository extends JpaRepository<Event, Integer> {
    List<Event> findAll();
}

Hibernate 生成查询:

select post0_.id as id2_4_, post0_.bodyl as body_bod3_4_, post0_.title as title12_4_ 
from post post0_ 
where post0_.dtype='EVENT'

这当然会生成错误,因为表中不存在“dtype”。

奇怪的是,如果我在 Post 实体上使用 @DiscriminatorFormula("post_type") ,一切似乎都有效。然而它速度较慢,所以我更喜欢使用@DiscriminatorColumn,因为它应该完全满足我的需求。

我正在使用 Hibernate 5.2.10-FINAL 和 Spring Data JPA 1.11.4(或者通常是最新的)。

有什么想法可能导致这种情况吗?

java hibernate jpa single-table-inheritance discriminator
2个回答
2
投票

我认为你遇到这个问题是因为你指定了错误的

@DiscriminatorColumn
注释参数,你应该使用
name
而不是
columnDefinition


0
投票

我指定了名称,但仍然收到相同的错误:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.