Spring JPA InheritanceType.JOINED如何解析记录类型



在我的spring应用程序中,我有一个类似于以下的实体层次结构:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
class A
@Entity
class B extends A
@Entity
class C extends A
@Entity
class D extends A

我想要一个jpa存储库方法,它将更新查询参数给定类型的所有记录。我已经创建了这样的方法:

@Modifying    
@Query("UPDATE A AS a SET a.someAttr = 1 WHERE TYPE(a.class) = (:clazz)")  
void updateRecordsOfType(@Param("clazz") Class<? extends A> clazz)

但它不起作用。它抛出org.h2.jdbc.JdbcSQLSyntaxErrorException: Column "A0_1_.ID" not found;在生成查询时,Hibernate似乎不连接表B、C和D。

当我在A课上使用@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)时,效果很好。

使用@Inheritance(strategy = InheritanceType.JOIN)?时,是否有任何方法可以解析记录类型

====================更新====================
我怀疑这可能是Hibernate中的错误。我启用了sql日志记录,发现Hibernate生成的查询不正确。为了进行比较,这是一个由ARepository.findAll((方法生成的查询:

select
a0_.id as id1_0_,
a0_.some_attr as some_att2_0_,
case 
when a0_1_.id is not null then 1 
when a0_2_.id is not null then 2 
when a0_3_.id is not null then 3 
when a0_.id is not null then 0 
end as clazz_ 
from
a a0_ 
left outer join
b a0_1_ 
on a0_.id=a0_1_.id 
left outer join
c a0_2_ 
on a0_.id=a0_2_.id 
left outer join
d a0_3_ 
on a0_.id=a0_3_.id

并且这是通过上面定义的方法CCD_ 4生成的查询:

update
a 
set
some_attr='someValue' 
where
case 
when a0_1_.id is not null then 1 
when a0_2_.id is not null then 2 
when a0_3_.id is not null then 3 
when id is not null then 0 
end=?

它似乎只是部分生成的,因为第一个查询中的FROM和JOIN子句丢失了。这就是抛出org.h2.jdbc.JdbcSQLSyntaxErrorException: Column "A0_1_.ID" not found;的原因。方法updateRecordsOfType用@Query((进行注释,其值包含带有TYPE((函数的查询。

总之,在我使用TYPE((函数之前,一切都很好。我无法解决这个问题我放弃了InheritanceType.JOINED,使用了InheridanceType.TABLE_PER_CLASS.

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "typeA")
class A
@Entity
@DiscriminatorValue("B")
class B extends A
@Entity
@DiscriminatorValue("C")
class C extends A
@Entity
@DiscriminatorValue("D")
class D extends A {}

我在使用这个继承时也遇到了同样的问题,但找不到我的方法,暂时我使用Inheritance(strategy = InheritanceType.SINGLE_TABLE(=>但是,所有记录都将保存在同一个实体上。

最新更新