JPA Query DSL连接两个没有主外键关系的表

时间:2018-08-14 12:38:35

标签: jpa spring-data-jpa querydsl

我正在尝试使用JPAQuery com.querydsl.jpa.impl.JPAQuery合并两个实体结果

有两个实体JobDetails和JobHistory。两个表之间没有明确的关联。

JobDetails具有一个临时字段,其最新的作业历史记录类型为JobHistory。两个表都将在两个公共属性Job Name和Job Group上联接。

QuartzJobDetails具有瞬态字段,如下所示,因为没有到DB的映射:

@Transient
@QueryType(PropertyType.SIMPLE)
private QuartzJobHistory latestQuartzJobHistory;


JPA Query code :

QQuartzJobDetails jobDetails = QQuartzJobDetails.quartzJobDetails;

QQuartzJobHistory jobHistory = QQuartzJobHistory.quartzJobHistory;


Predicate jobDetailAndHistoryPredicate = jobDetails.jobName.eq(.leftJoin(jobDetails.latestQuartzJobHistory,jobHistory)jobHistory.jobName).and(jobDetails.jobGroup.eq(jobHistory.jobGroup));

jpaQuery.from(jobDetails,jobHistory)
                .on(jobDetailAndHistoryPredicate)
                .orderBy(jobHistory.endTime.desc())
                .limit(pageable.getPageSize()).offset(pageable.getOffset());


Desired DB Query that needs to resolve from the above code :


select * from qrtz_job_details left join qrtz_cus_job_history on  qrtz_job_details.JOB_NAME = qrtz_cus_job_history.JOB_NAME and qrtz_job_details.JOB_GROUP = qrtz_cus_job_history.JOB_GROUP

0 个答案:

没有答案