Search in sources :

Example 16 with TypedQuery

use of javax.persistence.TypedQuery in project UVMS-ActivityModule-APP by UnionVMS.

the class FaReportDocumentDao method getFaReportDocumentsForTrip.

@SuppressWarnings("unchecked")
public List<FaReportDocumentEntity> getFaReportDocumentsForTrip(String tripId) {
    TypedQuery query = getEntityManager().createNamedQuery(FaReportDocumentEntity.FIND_FA_DOCS_BY_TRIP_ID, FaReportDocumentEntity.class);
    query.setParameter(TRIP_ID, tripId);
    return query.getResultList();
}
Also used : TypedQuery(javax.persistence.TypedQuery)

Example 17 with TypedQuery

use of javax.persistence.TypedQuery in project UVMS-ActivityModule-APP by UnionVMS.

the class FishingTripIdentifierDao method getNextTrips.

public List<FishingTripIdentifierEntity> getNextTrips(String vesselId, String vesselSchemeId, String tripId, Integer limit) {
    TypedQuery query = getEntityManager().createNamedQuery(FishingTripIdentifierEntity.FIND_NEXT_TRIP, FishingTripIdentifierEntity.class);
    query.setParameter(VESSEL_ID, vesselId);
    query.setParameter(VESSEL_SCHEME_ID, vesselSchemeId);
    query.setParameter(TRIP_ID, tripId);
    query.setMaxResults(limit);
    return query.getResultList();
}
Also used : TypedQuery(javax.persistence.TypedQuery)

Example 18 with TypedQuery

use of javax.persistence.TypedQuery in project hibernate-orm by hibernate.

the class SingularAttributeJoinTest method testEntityModeMapJoinCriteriaQuery.

@Test
public void testEntityModeMapJoinCriteriaQuery() throws Exception {
    final EntityManager entityManager = entityManagerFactory().createEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery criteriaQuery = criteriaBuilder.createQuery();
    javax.persistence.metamodel.EntityType distributionEntity = getEntityType("Distribution");
    From distributionFrom = criteriaQuery.from(distributionEntity);
    From policyJoin = distributionFrom.join("policy");
    Path policyId = policyJoin.get("policyId");
    criteriaQuery.select(policyId);
    TypedQuery typedQuery = entityManager.createQuery(criteriaQuery);
// typedQuery.getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Path(javax.persistence.criteria.Path) EntityManager(javax.persistence.EntityManager) TypedQuery(javax.persistence.TypedQuery) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) From(javax.persistence.criteria.From) Test(org.junit.Test)

Example 19 with TypedQuery

use of javax.persistence.TypedQuery in project hibernate-orm by hibernate.

the class CriteriaCompiler method compile.

public QueryImplementor compile(CompilableCriteria criteria) {
    try {
        criteria.validate();
    } catch (IllegalStateException ise) {
        throw new IllegalArgumentException("Error occurred validating the Criteria", ise);
    }
    final Map<ParameterExpression<?>, ExplicitParameterInfo<?>> explicitParameterInfoMap = new HashMap<>();
    final List<ImplicitParameterBinding> implicitParameterBindings = new ArrayList<>();
    final SessionFactoryImplementor sessionFactory = entityManager.getSessionFactory();
    final LiteralHandlingMode criteriaLiteralHandlingMode = sessionFactory.getSessionFactoryOptions().getCriteriaLiteralHandlingMode();
    final Dialect dialect = sessionFactory.getServiceRegistry().getService(JdbcServices.class).getDialect();
    RenderingContext renderingContext = new RenderingContext() {

        private int aliasCount;

        private int explicitParameterCount;

        public String generateAlias() {
            return "generatedAlias" + aliasCount++;
        }

        public String generateParameterName() {
            return "param" + explicitParameterCount++;
        }

        @Override
        @SuppressWarnings("unchecked")
        public ExplicitParameterInfo registerExplicitParameter(ParameterExpression<?> criteriaQueryParameter) {
            ExplicitParameterInfo parameterInfo = explicitParameterInfoMap.get(criteriaQueryParameter);
            if (parameterInfo == null) {
                if (StringHelper.isNotEmpty(criteriaQueryParameter.getName())) {
                    parameterInfo = new ExplicitParameterInfo(criteriaQueryParameter.getName(), null, criteriaQueryParameter.getJavaType());
                } else if (criteriaQueryParameter.getPosition() != null) {
                    parameterInfo = new ExplicitParameterInfo(null, criteriaQueryParameter.getPosition(), criteriaQueryParameter.getJavaType());
                } else {
                    parameterInfo = new ExplicitParameterInfo(generateParameterName(), null, criteriaQueryParameter.getJavaType());
                }
                explicitParameterInfoMap.put(criteriaQueryParameter, parameterInfo);
            }
            return parameterInfo;
        }

        public String registerLiteralParameterBinding(final Object literal, final Class javaType) {
            final String parameterName = generateParameterName();
            final ImplicitParameterBinding binding = new ImplicitParameterBinding() {

                public String getParameterName() {
                    return parameterName;
                }

                public Class getJavaType() {
                    return javaType;
                }

                public void bind(TypedQuery typedQuery) {
                    typedQuery.setParameter(parameterName, literal);
                }
            };
            implicitParameterBindings.add(binding);
            return parameterName;
        }

        public String getCastType(Class javaType) {
            SessionFactoryImplementor factory = entityManager.getFactory();
            Type hibernateType = factory.getTypeResolver().heuristicType(javaType.getName());
            if (hibernateType == null) {
                throw new IllegalArgumentException("Could not convert java type [" + javaType.getName() + "] to Hibernate type");
            }
            return hibernateType.getName();
        }

        @Override
        public Dialect getDialect() {
            return dialect;
        }

        @Override
        public LiteralHandlingMode getCriteriaLiteralHandlingMode() {
            return criteriaLiteralHandlingMode;
        }
    };
    return criteria.interpret(renderingContext).buildCompiledQuery(entityManager, new InterpretedParameterMetadata() {

        @Override
        public Map<ParameterExpression<?>, ExplicitParameterInfo<?>> explicitParameterInfoMap() {
            return explicitParameterInfoMap;
        }

        @Override
        public List<ImplicitParameterBinding> implicitParameterBindings() {
            return implicitParameterBindings;
        }
    });
}
Also used : HashMap(java.util.HashMap) TypedQuery(javax.persistence.TypedQuery) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) LiteralHandlingMode(org.hibernate.query.criteria.LiteralHandlingMode) ArrayList(java.util.ArrayList) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) Type(org.hibernate.type.Type) ParameterExpression(javax.persistence.criteria.ParameterExpression) Dialect(org.hibernate.dialect.Dialect) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 20 with TypedQuery

use of javax.persistence.TypedQuery in project hibernate-orm by hibernate.

the class CriteriaToScrollableResultsFetchTest method getOrderLinesScrolled.

private List<OrderLine> getOrderLinesScrolled(Long facilityId) {
    EntityManager em = getOrCreateEntityManager();
    try {
        em.getTransaction().begin();
        Set<PurchaseOrg> purchaseOrgs = getPurchaseOrgsByFacilityId(facilityId, em);
        assertEquals("Expected one purchase organization.", 1, purchaseOrgs.size());
        System.out.println(purchaseOrgs);
        TypedQuery<OrderLine> query = getOrderLinesQuery(purchaseOrgs, em);
        Query hibernateQuery = query.unwrap(Query.class);
        hibernateQuery.setReadOnly(true);
        hibernateQuery.setCacheable(false);
        List<OrderLine> lines = new ArrayList<>();
        ScrollableResults scrollableResults = hibernateQuery.scroll();
        scrollableResults.last();
        int rows = scrollableResults.getRowNumber() + 1;
        scrollableResults.beforeFirst();
        while (scrollableResults.next()) {
            lines.add((OrderLine) scrollableResults.get(0));
        }
        assertNotNull(lines);
        assertEquals("Expected one order line", 1, lines.size());
        em.getTransaction().commit();
        return lines;
    } catch (Throwable t) {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        throw t;
    } finally {
        em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) TypedQuery(javax.persistence.TypedQuery) Query(org.hibernate.Query) ArrayList(java.util.ArrayList) ScrollableResults(org.hibernate.ScrollableResults)

Aggregations

TypedQuery (javax.persistence.TypedQuery)52 EntityManager (javax.persistence.EntityManager)28 List (java.util.List)24 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)24 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)21 ArrayList (java.util.ArrayList)16 Query (javax.persistence.Query)14 Predicate (javax.persistence.criteria.Predicate)14 Root (javax.persistence.criteria.Root)13 Map (java.util.Map)10 LinkedList (java.util.LinkedList)9 NoResultException (javax.persistence.NoResultException)9 Collectors (java.util.stream.Collectors)8 UserTransaction (javax.transaction.UserTransaction)8 Item (org.jpwh.model.querying.Item)8 Test (org.testng.annotations.Test)8 EntityTransaction (javax.persistence.EntityTransaction)6 BigDecimal (java.math.BigDecimal)5 Date (java.util.Date)5 Set (java.util.Set)5