Hibernate Criteria JPA 子查询 exists 代码示例

select
	department0_.ID
from
	ORGAN_DEPARTMENT department0_
where
	exists (
	select
		'X'
	from
		FLOW_APPLY apply1_
	inner join FLOW_WORKFLOW flow2_ on
		apply1_.FK_FLOW = flow2_.ID
	inner join ORGAN_DEPARTMENT department3_ on
		flow2_.FK_DEPARTMENT_ID = department3_.ID
	where
		(exists (
			select
				'X'
			from
				FLOW_ANNEX attachment4_
			where
				attachment4_.TYPE_ = 'ATTACHMENT'
				and attachment4_.SUBMIT_METHOD ='ATTA'
				and attachment4_.FK_APPLY = apply1_.ID))
			and (apply1_.TYPE_ in ('Apply','cancel','proxy','EXTERNAL','EXTERNAL_PROXY'))
				and department3_.ID = department0_.ID)

以上是一个SQL  嵌套exists 的样例,相应的HIbernate Criteria Query 代码如下

通过CriteriaQuery#subquery创建子查询Subquery实例,然后通过CriteriaBuilder#exists加入条件  

"类名_"为Static Metadata Model类,通过Hibernate jpamodelgen模块生成

        CriteriaBuilder builder = session.getCriteriaBuilder();
        CriteriaQuery<Department> departQuery = builder.createQuery(Department.class);
        Root<Department> departmentRoot = departQuery.from(Department.class);
        Subquery<String> applyQuery = departQuery.subquery(String.class);
        Root<Apply> applyRoot = applyQuery.from(Apply.class);
        applyQuery.select(builder.literal("X"));
        Subquery<String> annexSubquery = applyQuery.subquery(String.class);
        Root<AttachmentAnnex> annexRoot = annexSubquery.from(AttachmentAnnex.class);
        annexSubquery.select(builder.literal("X"));
        annexSubquery.where(builder.equal(annexRoot.get(AttachmentAnnex_.submitMethod), AnnexDefinition.SubmitMethod.DEFICIENCY),
                builder.equal(annexRoot.get(AbstractApplyAnnex_.apply),applyRoot));
        Predicate typesPre = applyRoot.get(AbstractApply_.TYPE).in(List.of(ApplyQuery.EXTERNAL_APPLY_TYPES));
        applyQuery.where(builder.exists(annexSubquery), typesPre,
                builder.equal(applyRoot.join(AbstractApply_.flow).join(Flow_.department), departmentRoot));
        departQuery.where(builder.exists(applyQuery));
        List<Department> deptList = session.createQuery(departQuery).setReadOnly(true).setComment("事项所属部门-查询有容缺提交的部门").list();
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

victorkevin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值