我目前遇到设置谓词的问题,其中集合使用的是InheritanceType.JOINED。我目前正在使用QueryDSL 3.6.3。
我试图创建一个基类的谓词,如下所示:
final QCollector collector = QCollector.collector;
final JPAQuery query = new JPAQuery(entityManager);
query.from(collector).where(collector.parents.any().desc.eq("test"));
final List<Collector> results = query.list(collector);
生成此JPQL:
select collector
from Collector collector
where exists (select 1 from collector.parents as collector_parents_1f637
where collector_parents_1f637.desc = ?1)
并导致以下SQL
select collector0_.ID as ID1_1_ from COLLECTOR collector0_ where exists (select 1 from PARENT parents1_ where collector0_.ID=parents1_.PARENT_ID and parents1_1_.DESC=?)
此SQL不正确,因为它未加入BASE for DESC。我的JPA映射中是否存在一些错误,因为我错误地构建了谓词?
示例代码
@Entity
@Table(name = "BASE")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Base implements Serializable {
@Column(name = "BASE_ID")
@Id
private Long id;
@Column(name = "CODE")
private String code;
@Column(name = "DESC")
private String desc;
...
@Entity
@Table(name = "PARENT")
@PrimaryKeyJoinColumn(name = "BASE_ID", referencedColumnName = "BASE_ID")
public class Parent extends Base {
@Column(name = "PARENT_ID") private Long parentId;
...
@Entity@Table(name = "COLLECTOR")
public class Collector implements Serializable {
@Column(name = "ID")
@Id
private Long id;
@OneToMany(mappedBy = "parentId")
private List<Parent> parents;
....
CREATE TABLE BASE (
BASE_ID NUMERIC(18,0) NOT NULL,
CODE VARCHAR(10),
DESC VARCHAR(200),
PRIMARY KEY (BASE_ID)
);
CREATE TABLE PARENT (
BASE_ID NUMERIC(18,0) NOT NULL,
PARENT_ID NUMERIC(18,0) NOT NULL,
PRIMARY KEY (BASE_ID)
);
CREATE TABLE COLLECTOR (
ID NUMERIC(18,0) NOT NULL,
PARENT_ID NUMERIC(18,0) NOT NULL,
PRIMARY KEY (ID)
);