@Test public void testMultiSelectedClause() { String expected = "Select p.personName,p.age from Person p"; CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder(); // Check for multi column select CriteriaQuery<Person> personQuery = criteriaBuilder.createQuery(Person.class); Root<Person> from = personQuery.from(Person.class); personQuery.multiselect((Selection) from.get("personName").alias("p"), (Selection) from.get("age").alias("p")); String actual = CriteriaQueryTranslator.translate(personQuery); Assert.assertEquals(expected.trim(), actual.trim()); }
@Test public void testSelectedClause() { String expected = "Select p.personName from Person p"; CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder(); // Check for multi column select CriteriaQuery<Person> personQuery = criteriaBuilder.createQuery(Person.class); Root<Person> from = personQuery.from(Person.class); personQuery.select((Selection) from.get("personName").alias("p")); String actual = CriteriaQueryTranslator.translate(personQuery); Assert.assertEquals(expected.trim(), actual.trim()); }
private List<Selection<?>> toSelectionsList(List<SingularAttribute<T, ?>> attributes, boolean setAlias) { List<Selection<?>> selections = new ArrayList<>(attributes.size()); for (SingularAttribute<T, ?> attr : attributes) { Path<?> path = getRoot().get(attr); path.alias(attr.getName()); selections.add(path); } return selections; }
private List<Selection<?>> toSelectionsList(List<SingularAttribute<T, ?>> attributes, boolean setAlias) { List<Selection<?>> selections = new ArrayList<>(attributes.size()); for (SingularAttribute<T, ?> attr : attributes) { Path<?> path = getRoot().get(attr); path.alias(attr.getName()); selections.add(path); } return selections; }
private List<Selection<?>> toSelectionsList(List<SingularAttribute<T, ?>> attributes, boolean setAlias) { List<Selection<?>> selections = new ArrayList<Selection<?>>(attributes.size()); for (SingularAttribute<T, ?> attr : attributes) { Path<?> path = getRoot().get(attr); path.alias(attr.getName()); selections.add(path); } return selections; }
@Override public CriteriaQuery<Integer> apply(CriteriaBuilder cb) { final CriteriaQuery<Integer> criteriaQuery = cb.createQuery(VersionImpl_.local.getBindableJavaType()); final Root<VersionImpl> versionRoot = criteriaQuery.from(VersionImpl.class); criteriaQuery.select( versionRoot .get(VersionImpl_.local) .alias(VersionImpl_.local.getName())); criteriaQuery.where( cb.equal( versionRoot.get(VersionImpl_.product), productParameter)); return criteriaQuery; } });
public static List<Dashboard> findDashboardsByOwnerMeta(EntityManager em, PrincipalUser user, String version) { requireArgument(em != null, "Entity manager can not be null."); try { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Tuple> cq = cb.createTupleQuery(); Root<Dashboard> e = cq.from(Dashboard.class); List<Selection<?>> fieldsToSelect = new ArrayList<>(); for(Field field : FieldUtils.getFieldsListWithAnnotation(Dashboard.class, Metadata.class)) { fieldsToSelect.add(e.get(field.getName()).alias(field.getName())); } cq.multiselect(fieldsToSelect); cq.where(cb.equal(e.get("owner"), user),version==null?cb.isNull(e.get("version")):cb.equal(e.get("version"), version)); return _readDashboards(em, cq, null); } catch (NoResultException ex) { return new ArrayList<>(0); } }
@Override public CriteriaQuery<Tuple> apply(CriteriaBuilder cb) { final CriteriaQuery<Tuple> criteriaQuery = cb.createTupleQuery(); final Root<VersionImpl> versionRoot = criteriaQuery.from(VersionImpl.class); criteriaQuery.multiselect( versionRoot .get(VersionImpl_.major) .alias(VersionImpl_.major.getName()), versionRoot .get(VersionImpl_.minor) .alias(VersionImpl_.minor.getName()), versionRoot .get(VersionImpl_.patch) .alias(VersionImpl_.patch.getName())); criteriaQuery.where( cb.equal( versionRoot.get(VersionImpl_.product), productParameter)); return criteriaQuery; } });
public static List<Dashboard> findDashboardsByOwnerMeta(EntityManager em, PrincipalUser user, String version) { requireArgument(em != null, "Entity manager can not be null."); try { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Tuple> cq = cb.createTupleQuery(); Root<Dashboard> e = cq.from(Dashboard.class); List<Selection<?>> fieldsToSelect = new ArrayList<>(); for(Field field : FieldUtils.getFieldsListWithAnnotation(Dashboard.class, Metadata.class)) { fieldsToSelect.add(e.get(field.getName()).alias(field.getName())); } cq.multiselect(fieldsToSelect); cq.where(cb.equal(e.get("owner"), user),version==null?cb.isNull(e.get("version")):cb.equal(e.get("version"), version)); return _readDashboards(em, cq, null); } catch (NoResultException ex) { return new ArrayList<>(0); } }
public static List<Selection<?>> createFieldsSelect(Root<?> r, QueryParameters q, String idField) { List<Selection<?>> fields = q.getFields().stream().distinct().map(f -> { String[] fSplit = f.split("\\."); Path<?> p = null; for (String fS : fSplit) { try { p = p == null ? r.get(fS) : p.get(fS); } catch (IllegalArgumentException e) { throw new NoSuchEntityFieldException(e.getMessage(), f, r.getJavaType().getSimpleName()); } } if (p == null) { throw new NoSuchEntityFieldException("", f, r.getJavaType().getSimpleName()); } return p.alias(f); }).collect(Collectors.toList()); try { boolean exists = fields.stream().anyMatch(f -> f.getAlias().equals(idField)); if (!exists) { fields.add(r.get(idField).alias(idField)); } } catch (IllegalArgumentException e) { throw new NoSuchEntityFieldException(e.getMessage(), idField, r.getJavaType().getSimpleName()); } return fields.stream().distinct().collect(Collectors.toList()); }
public static List<Dashboard> findDashboardsMeta(EntityManager em, Integer limit, String version) { requireArgument(em != null, "Entity manager can not be null."); try { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Tuple> cq = cb.createTupleQuery(); Root<Dashboard> e = cq.from(Dashboard.class); List<Selection<?>> fieldsToSelect = new ArrayList<>(); for(Field field : FieldUtils.getFieldsListWithAnnotation(Dashboard.class, Metadata.class)) { fieldsToSelect.add(e.get(field.getName()).alias(field.getName())); } cq.multiselect(fieldsToSelect); if(version==null) { cq.where(cb.isNull(e.get("version"))); } else { cq.where(cb.equal(e.get("version"), version)); } return _readDashboards(em, cq, limit); } catch (NoResultException ex) { return new ArrayList<>(0); } }
fieldsToSelect.add(e.get(field.getName()).alias(field.getName()));
public static List<Dashboard> findDashboardsMeta(EntityManager em, Integer limit, String version) { requireArgument(em != null, "Entity manager can not be null."); try { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Tuple> cq = cb.createTupleQuery(); Root<Dashboard> e = cq.from(Dashboard.class); List<Selection<?>> fieldsToSelect = new ArrayList<>(); for(Field field : FieldUtils.getFieldsListWithAnnotation(Dashboard.class, Metadata.class)) { fieldsToSelect.add(e.get(field.getName()).alias(field.getName())); } cq.multiselect(fieldsToSelect); if(version==null) { cq.where(cb.isNull(e.get("version"))); } else { cq.where(cb.equal(e.get("version"), version)); } return _readDashboards(em, cq, limit); } catch (NoResultException ex) { return new ArrayList<>(0); } }
private List<Selection<?>> buildExpandJoinPath() throws ODataApplicationException { final List<Selection<?>> selections = new ArrayList<>(); try { final List<JPAOnConditionItem> associationPathList = assoziation.getJoinColumnsList(); for (JPAOnConditionItem onCondition : associationPathList) { final Path<?> p = ExpressionUtil.convertToCriteriaPath(target, onCondition.getRightPath().getPath()); p.alias(onCondition.getRightPath().getAlias()); selections.add(p); } } catch (ODataJPAModelException e) { throw new ODataJPAQueryException(e, HttpStatusCode.BAD_REQUEST); } return selections; }
@Override protected List<Selection<?>> createSelectClause(final Map<String, From<?, ?>> joinTables, // NOSONAR final List<JPAPath> jpaPathList, final From<?, ?> target) throws ODataApplicationException { // NOSONAR Allow // subclasses to throw an exception final int handle = debugger.startRuntimeMeasurement(this, "createSelectClause"); final List<Selection<?>> selections = new ArrayList<>(); // Based on an error in Eclipse Link first the join columns have to be selected. Otherwise the alias is assigned to // the wrong column. E.g. if Organization Comment shall be read Eclipse Link automatically selects also the Order // column and if the join column is added later the select clause would look as follows: SELECT t0."Text, // t0."Order", t1,"ID". Eclipse Link will then return the value of the Order column for the alias of the ID column. createAdditionSelctionForJoinTable(selections); // Build select clause for (final JPAPath jpaPath : jpaPathList) { final Path<?> p = ExpressionUtil.convertToCriteriaPath(joinTables, target, jpaPath.getPath()); p.alias(jpaPath.getAlias()); selections.add(p); } debugger.stopRuntimeMeasurement(handle); return selections; }
private void createAdditionSelctionForJoinTable(final List<Selection<?>> selections) throws ODataJPAQueryException { final From<?, ?> parent = determineParentFrom(); // e.g. JoinSource try { for (JPAPath p : assoziation.getLeftColumnsList()) { final Path<?> selection = ExpressionUtil.convertToCriteriaPath(parent, p.getPath()); // If source and target of an association use the same name for their key we get conflicts with the alias. // Therefore it is necessary to unify them. selection.alias(assoziation.getAlias() + ALIAS_SEPERATOR + p.getAlias()); selections.add(selection); } } catch (ODataJPAModelException e) { throw new ODataJPAQueryException(e, HttpStatusCode.INTERNAL_SERVER_ERROR); } }
private void createAdditionSelctionForJoinTable(final List<Selection<?>> selections) throws ODataJPAQueryException { final From<?, ?> parent = determineParentFrom(); // e.g. JoinSource try { for (JPAPath p : assoziation.getLeftColumnsList()) { final Path<?> selection = ExpressionUtil.convertToCriteriaPath(parent, p.getPath()); // If source and target of an association use the same name for their key we get conflicts with the alias. // Therefore it is necessary to unify them. selection.alias(assoziation.getAlias() + ALIAS_SEPERATOR + p.getAlias()); selections.add(selection); } } catch (ODataJPAModelException e) { throw new ODataJPAQueryException(e, HttpStatusCode.INTERNAL_SERVER_ERROR); } }
@Test public void testFetchObjectArrayToDTO() { doInJPA(entityManager -> { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Object[]> criteria = builder.createQuery(Object[].class); Root<PostComment> root = criteria.from(PostComment.class); Join<PostComment, Post> postJoin = root.join("post"); criteria.multiselect( root.get(PostComment_.id).alias("id"), root.get(PostComment_.review).alias("review"), postJoin.get(Post_.title).alias("title") ); criteria.where(builder.like(postJoin.get(Post_.title), "high-performance%")); List<PostCommentSummary> comments = entityManager .createQuery(criteria) .unwrap(Query.class) .setResultTransformer(Transformers.aliasToBean(PostCommentSummary.class)) .getResultList(); assertEquals(5, comments.size()); }); } }