Spring DATA JPA嵌套子查询

JPA 子查询: select * from A where filed in (select filed from B) 

Specification<Role> specification = new Specification<Role>() {

	@Override
	public Predicate toPredicate(Root<Role> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
		
		Subquery<Long> subQuery = query.subquery(Long.class);
		Root<UserRole> fromUR = subQuery.from(UserRole.class);
		
		subQuery.select(fromUR.get("id").get("roleId")).where(cb.equal(fromUR.get("id").get("userId"), 2059));
		
		return cb.not(cb.in(root.get("roleId")).value(subQuery));
		
	}
};

对应SQL:

    select
        role0_.role_id as role_id1_4_,
        role0_.create_date as create_d2_4_,
        role0_.description as descript3_4_,
        role0_.modify_date as modify_d4_4_,
        role0_.name as name5_4_,
        role0_.role_key as role_key6_4_ 
    from
        role_ role0_ 
    where
        role0_.role_id not in  (
            select
                userrole1_.role_id 
            from
                user_role userrole1_ 
            where
                userrole1_.user_id=2059
        ) 
    order by
        role0_.name asc limit ?

 

转载于:https://my.oschina.net/u/3359/blog/1329953

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值