当连接子句的一部分需要硬编码值时,JPA 是否可以连接到代码表



我希望生成的实体包含表 1 中的所有列,以及代码 1 中的描述。

如果我在SQL中执行此操作,我会编写如下:

select table1.*, codes1.description
from table1
inner joing codes1 where codes1.code = table1.status_code 
                     and codes1.group = 'status'

我已经使用本机查询完成了此操作,但如果可能的话,我想使用直接的 JPA 来执行此操作。

代码表:

Group   Code  Description
status  a     status code a
status  b     status code b
other   a     other code a

如果我们想象 2 个对象:Table1 和 Code1。您的类 Table1 包含课程代码 1。

在"直接 JPA"或 jpql 中,选择一个对象,因此查询将是:

select t from Table1 t where t.code1.group = 'status'

连接由映射自动完成(@OneToOne,@ManyToOne...)。

最新更新