CriteriaQuery<Tuple> cq = builder.createTupleQuery(); // write the Root, Path elements as usual Root<EntityClazz> root = cq.from(EntityClazz.class); cq.multiselect(root.get(EntityClazz_.ID), root.get(EntityClazz_.VERSION)); //using metamodel List<Tuple> tupleResult = em.createQuery(cq).getResultList(); for (Tuple t : tupleResult) { Long id = (Long) t.get(0); Long version = (Long) t.get(1); }
@Override public Set<Entry<String, Object>> entrySet() { return tuple.getElements().stream() // .map(e -> new HashMap.SimpleEntry<String, Object>(e.getAlias(), tuple.get(e))) // .collect(Collectors.toSet()); } }
/** * If the key is not a {@code String} or not a key of the backing {@link Tuple} this returns {@code false}. * Otherwise this returns {@code true} even when the value from the backing {@code Tuple} is {@code null}. * * @param key the key for which to get the value from the map. * @return wether the key is an element of the backing tuple. */ @Override public boolean containsKey(Object key) { try { tuple.get((String) key); return true; } catch (IllegalArgumentException e) { return false; } }
@Override public Object convert(Object source) { if (!(source instanceof Tuple)) { return source; } Tuple tuple = (Tuple) source; List<TupleElement<?>> elements = tuple.getElements(); if (elements.size() == 1) { Object value = tuple.get(elements.get(0)); if (type.isInstance(value) || value == null) { return value; } } return new TupleBackedMap(tuple); }
/** * If the key is not a {@code String} or not a key of the backing {@link Tuple} this returns {@code null}. * Otherwise the value from the backing {@code Tuple} is returned, which also might be {@code null}. * * @param key the key for which to get the value from the map. * @return the value of the backing {@link Tuple} for that key or {@code null}. */ @Override @Nullable public Object get(Object key) { if (!(key instanceof String)) { return null; } try { return tuple.get((String) key); } catch (IllegalArgumentException e) { return null; } }
private static Map<Integer, Integer> buildColumnSizesFromRows(List<Tuple> rows) { Map<Integer, Integer> columnSizes = new TreeMap<>(); for ( int i = 0; i < rows.size(); ++i ) { Tuple row = rows.get( i ); // on first row, handle columns if ( i == 0 ) { for ( int j = 0; j < row.getElements().size(); ++j ) { TupleElement<?> element = row.getElements().get( j ); columnSizes.put( j, element.getAlias().length() ); } } // handle values for ( int j = 0; j < row.getElements().size(); ++j ) { Object value = row.get( j ); int length = ( "" + value ).trim().length(); if ( length > columnSizes.get( j ) ) { columnSizes.put( j, length ); } } } return columnSizes; }
tupleRow.get(i++, Long.class), tupleRow.get(i++, String.class), tupleRow.get(i++, String.class), tupleRow.get(i++, String.class), tupleRow.get(i++, Status.class), tupleRow.get(i++, Integer.class), tupleRow.get(i++, Boolean.class), tupleRow.get(i++, String.class), tupleRow.get(i++, String.class), tupleRow.get(i++, Date.class), tupleRow.get(i++, Date.class), tupleRow.get(i++, Date.class), tupleRow.get(i++, String.class), tupleRow.get(i++, Long.class), tupleRow.get(i++, Long.class), tupleRow.get(i++, String.class), tupleRow.get(i++, SubTasksStrategy.class), tupleRow.get(i++, Long.class) );
private static void printTableFromRows(List<Tuple> rows) { if ( rows.size() > 0 ) { Map<Integer, Integer> columnSizes = buildColumnSizesFromRows( rows ); String rowSplitter = null; for ( int i = 0; i < rows.size(); ++i ) { Tuple row = rows.get( i ); List<TupleElement<?>> columns = row.getElements(); if ( rowSplitter == null ) { rowSplitter = buildSplitter( columns, columnSizes ); } // handle columns on first row if ( i == 0 ) { System.out.println( rowSplitter ); for ( int j = 0; j < columns.size(); ++j ) { writeColumn( j, columns.size(), columnSizes.get( j ), columns.get( j ).getAlias() ); } System.out.println( rowSplitter ); } // handle column values for ( int j = 0; j < columns.size(); ++j ) { final String value = row.get( j ) + "".trim(); writeColumn( j, columns.size(), columnSizes.get( j ), value ); } } System.out.println( rowSplitter ); } }
@Test public void test_criteria_tuple_example() { doInJPA( this::entityManagerFactory, entityManager -> { //tag::criteria-tuple-example[] CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Tuple> criteria = builder.createQuery( Tuple.class ); Root<Person> root = criteria.from( Person.class ); Path<Long> idPath = root.get( Person_.id ); Path<String> nickNamePath = root.get( Person_.nickName); criteria.multiselect( idPath, nickNamePath ); criteria.where( builder.equal( root.get( Person_.name ), "John Doe" ) ); List<Tuple> tuples = entityManager.createQuery( criteria ).getResultList(); for ( Tuple tuple : tuples ) { Long id = tuple.get( idPath ); String nickName = tuple.get( nickNamePath ); } //or using indices for ( Tuple tuple : tuples ) { Long id = (Long) tuple.get( 0 ); String nickName = (String) tuple.get( 1 ); } //end::criteria-tuple-example[] assertEquals(1, tuples.size()); }); }
@Test public void testHibernateSelectClauseFunction() { doInJPA( this::entityManagerFactory, entityManager -> { //tag::hql-user-defined-function-postgresql-select-clause-example[] List<Tuple> books = entityManager.createQuery( "select b.title as title, apply_vat(b.priceCents) as price " + "from Book b " + "where b.author = :author ", Tuple.class ) .setParameter( "author", "Vlad Mihalcea" ) .getResultList(); assertEquals( 1, books.size() ); Tuple book = books.get( 0 ); assertEquals( "High-Performance Java Persistence", book.get( "title" ) ); assertEquals( 5400, ((Number) book.get( "price" )).intValue() ); //end::hql-user-defined-function-postgresql-select-clause-example[] }); }
@Test public void test_criteria_group_by_example() { doInJPA( this::entityManagerFactory, entityManager -> { //tag::criteria-group-by-example[] CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Tuple> criteria = builder.createQuery( Tuple.class ); Root<Person> root = criteria.from( Person.class ); criteria.groupBy(root.get("address")); criteria.multiselect(root.get("address"), builder.count(root)); List<Tuple> tuples = entityManager.createQuery( criteria ).getResultList(); for ( Tuple tuple : tuples ) { String name = (String) tuple.get( 0 ); Long count = (Long) tuple.get( 1 ); } //end::criteria-group-by-example[] assertEquals(2, tuples.size()); }); }
@Override public void before() { Tuple tuple = SQL.New("select nic.ip, nic.vmInstanceUuid, nic.uuid, eip.vipIp from VmNicVO nic, EipVO eip" + " where eip.vmNicUuid = nic.uuid and eip.uuid = :uuid", Tuple.class) .param("uuid", getEipUuid()).find(); ip = tuple.get(0, String.class); vmUuid = tuple.get(1, String.class); vmNicUuid = tuple.get(2, String.class); eip = tuple.get(3, String.class); }
public void checkRunningVmForUpdateUrl(String psuuid) { String sql = "select vm.name, vm.uuid from VmInstanceVO vm, VolumeVO vol where vm.uuid = vol.vmInstanceUuid and" + " vol.primaryStorageUuid = :psUuid and vm.state = :vmState"; TypedQuery<Tuple> q = dbf.getEntityManager().createQuery(sql, Tuple.class); q.setParameter("psUuid", psuuid); q.setParameter("vmState", VmInstanceState.Running); List<Tuple> ts = q.getResultList(); if (!ts.isEmpty()) { List<String> vms = ts.stream().map(v -> String.format("VM[name:%s, uuid:%s]", v.get(0, String.class), v.get(1, String.class))).collect(Collectors.toList()); throw new ApiMessageInterceptionException(operr("there are %s running VMs on the NFS primary storage, please" + " stop them and try again:\n%s\n", vms.size(), StringUtils.join(vms, "\n"))); } } }
@Override public void before() { Tuple tuple = SQL.New("select nic.ip, nic.vmInstanceUuid, nic.uuid from VmNicVO nic, PortForwardingRuleVO pfr" + " where pfr.vmNicUuid = nic.uuid and pfr.uuid = :uuid", Tuple.class) .param("uuid", getUuid()).find(); ip = tuple.get(0, String.class); vmUuid = tuple.get(1, String.class); vmNicUuid = tuple.get(2, String.class); }
CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Tuple> cq= cb.createTupleQuery(); Root<Documents> root = cq.from(Documents.class); Expression<Integer> userId = root.get("USERID"); Expression<String> userType = root.get("USERTYPE"); Expression<Long> count = cb.count(userId); cq.multiselect(userId.alias("USERID"), count.alias("CNT")); cq.where(cb.equal(userType, "COMPANY"); cq.groupBy(userId); cq.orderBy(cb.desc(count)); TypedQuery<Tuple> tq = em.createQuery(cq); for (Tuple t : tq.getResultsList()) { System.out.println(t.get("USERID")); System.out.println(t.get("CNT")); }
private Map<String, String> getUuidTypeMapByResourceUuids(List<String> resourceUuids) { List<Tuple> ts = Q.New(AccountResourceRefVO.class) .select(AccountResourceRefVO_.resourceUuid, AccountResourceRefVO_.resourceType) .in(AccountResourceRefVO_.resourceUuid, resourceUuids) .listTuple(); Map<String, String> uuidType = new HashMap<String, String>(); for (Tuple t : ts) { String resUuid = t.get(0, String.class); String resType = t.get(1, String.class); uuidType.put(resUuid, resType); } return uuidType; }
private void validate(APIGetPortForwardingAttachableVmNicsMsg msg) { SimpleQuery<PortForwardingRuleVO> q = dbf.createQuery(PortForwardingRuleVO.class); q.select(PortForwardingRuleVO_.state, PortForwardingRuleVO_.vmNicUuid); q.add(PortForwardingRuleVO_.uuid, Op.EQ, msg.getRuleUuid()); Tuple t = q.findTuple(); PortForwardingRuleState state = t.get(0, PortForwardingRuleState.class); if (state != PortForwardingRuleState.Enabled) { throw new ApiMessageInterceptionException(operr("Port forwarding rule[uuid:%s] is not in state of Enabled, current state is %s", msg.getRuleUuid(), state)); } String vmNicUuid = t.get(1, String.class); if (vmNicUuid != null) { return ; } }
private void validate(APIRevertVolumeFromSnapshotMsg msg) { SimpleQuery<VolumeSnapshotVO> q = dbf.createQuery(VolumeSnapshotVO.class); q.select(VolumeSnapshotVO_.state, VolumeSnapshotVO_.volumeUuid); q.add(VolumeSnapshotVO_.uuid, Op.EQ, msg.getUuid()); Tuple t = q.findTuple(); VolumeSnapshotState state = t.get(0, VolumeSnapshotState.class); if (state != VolumeSnapshotState.Enabled) { throw new ApiMessageInterceptionException(operr("volume snapshot[uuid:%s] is in state %s, cannot revert volume to it", msg.getUuid(), state)); } String volUuid = t.get(1, String.class); if (volUuid == null) { throw new ApiMessageInterceptionException(operr("original volume for snapshot[uuid:%s] has been deleted, cannot revert volume to it", msg.getUuid())); } }