简体   繁体   English

使用JPA条件创建动态查询

[英]Creating dynamic query using jpa criteria

I created dynamic query using jpa criteria but an extra pair of parentheses gets added to columns to be selected when I do userGroupSubquery.select(userGroupsRoot); 我使用jpa条件创建了动态查询,但是当我执行userGroupSubquery.select(userGroupsRoot);时,要在要选择的列上添加一对额外的括号。

generated query 产生的查询

select (securitygr3_.group_name, securitygr3_.user_name) from gm.security_groupings securitygr3_ where 1=1 and securitygr3_.user_name='xxx' and (securitygr3_.group_name in ('XYZ')) 从gm.security_groupings securitygr3_中选择(securitygr3_.group_name,securitygr3_.user_name)其中1 = 1和securitygr3_.user_name ='xxx'和(('XYZ')中的securitygr3_.group_name)

expected query: 预期查询:

select securitygr3_.group_name, securitygr3_.user_name from gm.security_groupings securitygr3_ where 1=1 and securitygr3_.user_name='xxx' and (securitygr3_.group_name in ('XYZ')) 从gm.security_groupings securitygr3_中选择securitygr3_.group_name,securitygr3_.user_name,其中1 = 1,securitygr3_.user_name ='xxx'和(('XYZ')中的securitygr3_.group_name)。

Subquery<SecurityGroupings> userGroupSubquery = secUsersQuery.subquery(SecurityGroupings.class);
            Root<SecurityGroupings> userGroupsRoot = userGroupSubquery.from(SecurityGroupings.class);
            Path<SecurityGroupingsId> secGroupId = userGroupsRoot.get("id");
            Path<SecurityUsers> secUsers = secGroupId.get("securityUsers_1");
            Path<SecurityUsers> securityUsers = secGroupId.get("securityUsers");
            Path<String> su_name = secUsers.get("name");
            Path<String> name = securityUsers.get("name");
            userGroupSubquery.select(userGroupsRoot);
            userGroupSubquery.getCompoundSelectionItems();



//userGroupSubquery.where(criteriaBuilder.equal(pet.get(SecurityGroupingsId_.id), root.<String>get("name")));
            Predicate restrictions3 = criteriaBuilder.conjunction();
            restrictions3 = criteriaBuilder.and(restrictions3, criteriaBuilder.and(criteriaBuilder.equal(su_name, dto.getUserId().trim().toUpperCase())));
            restrictions3 = criteriaBuilder.and(restrictions3, criteriaBuilder.and(name.in(userGroups)));
            userGroupSubquery.where(restrictions3);
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.exists(userGroupSubquery));
        }
        secUsersQuery.where(restrictions);

Its just that I get an extra pair of parentheses at select (securitygr3_.group_name, securitygr3_.user_name) from which gives me ora-00907 missing right parenthesis error. 只是我在select(securitygr3_.group_name,securitygr3_.user_name)处获得了一对额外的圆括号,从中给我ora-00907缺少正确的圆括号错误。 I am sure it is coming from userGroupSubquery.select(userGroupsRoot) but I am not sure why. 我确定它来自userGroupSubquery.select(userGroupsRoot),但不知道为什么。 Please help 请帮忙

I got the solution for the above. 我得到了上述解决方案。 When the entity has a composite key, then in JPA criteria instead of doing 当实体具有复合密钥时,则使用JPA标准,而不是

userGroupSubquery.select(userGroupsRoot);

we should do 我们应该做

userGroupSubquery.select(userGroupsRoot.get("id"));

where id is the composite id. 其中id是复合ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM