我刚刚遇到以下异常:
java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: new near line 1, column 81 [select new com.test.user.request.api.entity.UserRequestEntity(new com.test.user.request.api.entity.UserViewEntity(generatedAlias0.user.email, new com.test.user.request.api.entity.ContactViewEntity_(generatedAlias0.user.contact.firstName, generatedAlias0.user.contact.lastName))) from com.test.user.request.api.entity.UserRequestEntity as generatedAlias0 where ( upper(generatedAlias0.user.contact.lastName) like upper(:param0) ) and ( generatedAlias0.organization.id=:param1 )]
我正在使用JPA条件API对查询进行编码,该API看起来如下
final CriteriaBuilder criteriaBuilder = this.em.getCriteriaBuilder();
final CriteriaQuery<UserRequestEntity> query = criteriaBuilder.createQuery(UserRequestEntity.class);
final Root<UserRequestEntity> userRequest = this.createUserRequestRoot(query, true);
Path<UserViewEntity> user = userRequest.get(UserRequestEntity_.user);
Path<ContactViewEntity> contact = user.get(UserViewEntity_.contact);
CompoundSelection<ContactViewEntity_> contactSelection = criteriaBuilder.construct(ContactViewEntity_.class, contact.get(ContactViewEntity_.firstName), contact.get(ContactViewEntity_.lastName));
CompoundSelection<UserViewEntity> userSelection = criteriaBuilder.construct(UserViewEntity.class, user.get(UserViewEntity_.email), contactSelection);
CompoundSelection<UserRequestEntity> userRequestSelection = criteriaBuilder.construct(UserRequestEntity.class, userSelection);
query.select(userRequestSelection);
对于概述,我需要来自多个实体的9-10个字段。如果我在多选中得到,我需要一个具有9-10个参数的构造函数。我以为我可以使用化合物选择来解决它。
我知道还有另一种方法可以通过使用fetch join来初始化相关数据,但我对fetch join的问题是我有一个通用过滤器转换器将过滤器从UI转换为条件api谓词,如果有过滤器assotiated entitiy(userRequest.user.contact.firstName)转换器创建重复的连接,因为似乎连接实例不会被重用。
有没有办法让化合物选择工作?