List of usage examples for org.hibernate.query Query setParameterList
@Override Query<R> setParameterList(String name, Object[] values);
From source file:be.shad.tsqb.dao.TypeSafeQueryDaoImpl.java
License:Apache License
/** * {@inheritDoc}/*from ww w . j av a 2 s. c o m*/ */ @Override public <T> QueryResult<T> doQuery(TypeSafeRootQuery tsqbQuery, HibernateQueryConfigurer configurer) { HqlQuery hqlQuery = tsqbQuery.toHqlQuery(); Session currentSession = sessionFactory.getCurrentSession(); Query<Object[]> query = currentSession.createQuery(hqlQuery.getHql(), Object[].class); int position = 0; CollectionNamedParameter chunkedParam = null; for (Object param : hqlQuery.getParams()) { if (param instanceof NamedParameter) { NamedParameter named = (NamedParameter) param; if (isChunkedParam(named)) { if (chunkedParam != null) { throw new IllegalStateException( String.format("More than one batched param [%s, %s] was used in query [%s].", chunkedParam.getName(), named.getName(), query.getQueryString())); } // remember batched param to bind iterate and bind chunks later: chunkedParam = (CollectionNamedParameter) named; } else if (named.getValue() instanceof Collection) { query.setParameterList(named.getName(), (Collection<?>) named.getValue()); } else { query.setParameter(named.getName(), named.getValue()); } } else { query.setParameter(position++, param); } } if (tsqbQuery.getFirstResult() >= 0) { query.setFirstResult(tsqbQuery.getFirstResult()); } if (tsqbQuery.getMaxResults() > 0) { query.setMaxResults(tsqbQuery.getMaxResults()); } List<T> results = null; if (configurer != null) { configurer.beforeQuery(currentSession); configurer.configureQuery(query); try { results = listAll(query, hqlQuery, chunkedParam); } finally { configurer.afterQuery(currentSession); } } else { results = listAll(query, hqlQuery, chunkedParam); } return new QueryResult<>(results); }
From source file:be.shad.tsqb.dao.TypeSafeQueryDaoImpl.java
License:Apache License
/** * Lists the same query with an updated collection in the named param for the batched named param. *///from w ww .j ava2s. c o m @SuppressWarnings("unchecked") private <T> List<T> listAll(Query<Object[]> query, HqlQuery hqlQuery, CollectionNamedParameter chunkedParam) { List<Object[]> results; if (chunkedParam == null) { results = query.getResultList(); } else { results = new LinkedList<>(); int p = chunkedParam.getBatchSize(); List<Object> values = new ArrayList<>(p); Iterator<?> it = chunkedParam.getValue().iterator(); while (it.hasNext()) { values.add(it.next()); if (values.size() == p || !it.hasNext()) { query.setParameterList(chunkedParam.getName(), values); results.addAll(query.getResultList()); values.clear(); } } } if (hqlQuery.getResultTransformer() != null) { return (List<T>) hqlQuery.getResultTransformer().transformList(results); } else { return (List<T>) results; } }
From source file:com.romeikat.datamessie.core.base.dao.impl.StatisticsDao.java
License:Open Source License
public StatisticsSparseTable getStatistics(final SharedSessionContract ssc, final Collection<Long> sourceIds, final LocalDate published) { if (CollectionUtils.isEmpty(sourceIds) || published == null) { return new StatisticsSparseTable(); }//from w ww . j a va 2 s . c o m // Query final StringBuilder hql = new StringBuilder(); hql.append("SELECT s.sourceId, s.state, documents "); hql.append("FROM Statistics s "); hql.append("WHERE s.sourceId IN :_sourceIds "); hql.append("AND s.published = :_published "); hql.append("GROUP BY s.sourceId, s.state "); final Query<Object[]> query = ssc.createQuery(hql.toString(), Object[].class); query.setParameterList("_sourceIds", sourceIds); query.setParameter("_published", published); // Execute final List<Object[]> records = query.list(); // Postprocess final StatisticsSparseTable statistics = new StatisticsSparseTable(); for (final Object[] record : records) { final long sourceId = (Long) record[0]; final DocumentProcessingState state = (DocumentProcessingState) record[1]; final long number = (Long) record[2]; final DocumentsPerState documentsForState = new DocumentsPerState(); documentsForState.put(state, number); statistics.putValue(sourceId, published, documentsForState); } // Done return statistics; }
From source file:com.romeikat.datamessie.core.sync.service.template.withIdAndVersion.DeleteExecutor.java
License:Open Source License
private void delete(final StatelessSession rhsStatelessSession, final Collection<Long> rhsIds) { if (rhsIds.isEmpty()) { return;/* w w w . java 2s . c o m*/ } final String queryString = "DELETE FROM " + clazz.getSimpleName() + " WHERE id IN :_rhsIds"; final Query<?> query = rhsStatelessSession.createQuery(queryString); query.setParameterList("_rhsIds", rhsIds); query.executeUpdate(); }
From source file:com.wso2telco.services.dep.sandbox.dao.hibernate.HibernateCustomerInfoDAO.java
License:Open Source License
@Override public List<AttributeValues> getAttributeServices(String msisdn, Integer userID, String imsi, String[] schema) throws Exception { Session session = getSession();/*from ww w. ja va 2 s. c o m*/ List<AttributeValues> attributeValues = null; StringBuilder hql = new StringBuilder(); hql.append("SELECT "); hql.append("val "); hql.append("FROM "); hql.append("AttributeValues AS val, "); hql.append("APIServiceCalls AS calls, "); hql.append("APITypes AS api, "); hql.append("AttributeDistribution AS dist, "); hql.append("Attributes AS att "); hql.append("WHERE "); hql.append("api.id = calls.apiType.id "); hql.append("AND calls.apiServiceCallId = dist.serviceCall.apiServiceCallId "); hql.append("AND dist.distributionId = val.attributeDistribution.distributionId "); hql.append("AND att.attributeId = dist.attribute.attributeId "); hql.append("AND val.ownerdid = :userId "); hql.append("AND api.apiname = :apiName "); hql.append("AND calls.serviceName = :apiService "); hql.append("AND att.attributeName IN ( :schema)"); try { Query query = session.createQuery(hql.toString()); query.setParameter("apiName", RequestType.CUSTOMERINFO.toString()); query.setParameter("apiService", CustomerInfoRequestType.GETATTRIBUTE.toString()); query.setParameter("userId", userID); query.setParameterList("schema", schema); attributeValues = (List<AttributeValues>) query.getResultList(); } catch (Exception ex) { LOG.error("###Customer rInfo### Error in get customer attributes " + ex); throw ex; } return attributeValues; }
From source file:com.wso2telco.services.dep.sandbox.dao.hibernate.HibernateCustomerInfoDAO.java
License:Open Source License
@Override public boolean checkSchema(String[] schema) throws Exception { Session session = getSession();//from w ww . jav a 2s . c o m Attributes resultSet = null; List<AttributeValues> attributeValues = null; try { StringBuilder hql = new StringBuilder(); hql.append("SELECT "); hql.append("att.attributeName "); hql.append("FROM "); hql.append("Attributes AS att "); hql.append("WHERE "); hql.append("att.attributeName IN ( :schema)"); Query query = session.createQuery(hql.toString()); query.setParameterList("schema", schema); attributeValues = (List<AttributeValues>) query.getResultList(); if (attributeValues.size() == schema.length) { return true; } } catch (Exception ex) { LOG.error("###Customer Info### Error in getErrorResponse " + ex); throw ex; } return false; }
From source file:com.wso2telco.services.dep.sandbox.dao.hibernate.HibernateProvisioningDAO.java
License:Open Source License
@Override public ProvisionedServices getAlreadyProvisionedService(User user, List<String> statusCodes, ProvisionAllService provisionService, String phoneNumber) throws Exception { Session session = getSession();/*ww w. j av a2 s . c o m*/ ProvisionedServices provisionedService = null; try { StringBuilder hqlQueryBuilder = new StringBuilder(); hqlQueryBuilder.append("select provisionedService "); hqlQueryBuilder.append(" from ProvisionedServices provisionedService,"); hqlQueryBuilder.append(" Status status,"); hqlQueryBuilder.append(" ProvisionMSISDNServicesMap map,"); hqlQueryBuilder.append(" ManageNumber AS number,"); hqlQueryBuilder.append(" ProvisionAllService provisionServices,"); hqlQueryBuilder.append(" User user "); hqlQueryBuilder.append(" where "); hqlQueryBuilder.append(" provisionedService.status.id = status.id"); hqlQueryBuilder.append(" AND provisionedService.msisdnServiceMap.id = map.id"); hqlQueryBuilder.append(" AND map.msisdnId.id = number.id"); hqlQueryBuilder.append(" AND map.servicesId.id = provisionServices.id"); hqlQueryBuilder.append(" AND number.user.id = user.id"); hqlQueryBuilder.append(" AND number.user = :spUser"); hqlQueryBuilder.append(" AND status.code in (:statusCodeList)"); hqlQueryBuilder.append(" AND number.Number = :phoneNumber"); hqlQueryBuilder.append(" AND provisionServices.id = :provisionServiceId"); Query query = session.createQuery(hqlQueryBuilder.toString()); query.setParameter("spUser", user); query.setParameterList("statusCodeList", statusCodes); query.setParameter("phoneNumber", phoneNumber); query.setParameter("provisionServiceId", provisionService.getId()); provisionedService = (ProvisionedServices) query.getSingleResult(); } catch (NoResultException e) { return null; } catch (Exception ex) { LOG.error("###PROVISION### Error in getAlreadyProvisionedService", ex); throw ex; } return provisionedService; }
From source file:com.wso2telco.services.dep.sandbox.dao.hibernate.HibernateWalletDAO.java
License:Open Source License
@Override public List<AttributeValues> getTransactionValue(String endUserId, List<String> attribute, String tableName, Integer userId) throws Exception { Session session = getSession();//from ww w . j a v a 2 s . c om List<AttributeValues> resultSet = null; StringBuilder hql = new StringBuilder(); hql.append("SELECT "); hql.append("val "); hql.append("FROM "); hql.append("AttributeValues AS val, "); hql.append("APIServiceCalls AS calls, "); hql.append("APITypes AS api, "); hql.append("AttributeDistribution AS dist, "); hql.append("Attributes AS att, "); hql.append("ManageNumber AS number "); hql.append("WHERE "); hql.append("api.id = calls.apiType.id "); hql.append("AND calls.apiServiceCallId = dist.serviceCall.apiServiceCallId "); hql.append("AND dist.distributionId = val.attributeDistribution.distributionId "); hql.append("AND att.attributeId = dist.attribute.attributeId "); hql.append("AND api.apiname =:apiName "); hql.append("AND val.tobject =:tableName "); hql.append("AND number.id = val.ownerdid "); hql.append("AND number.Number =:number "); hql.append("AND number.user.id =:userId "); hql.append("AND att.attributeName IN ( :attributeName)"); try { Query query = session.createQuery(hql.toString()); query.setParameter("apiName", RequestType.WALLET.toString().toLowerCase()); query.setParameter("number", endUserId); query.setParameter("tableName", tableName); query.setParameterList("attributeName", attribute); query.setParameter("userId", userId); query.setFirstResult(0); query.setMaxResults(20); resultSet = (List<AttributeValues>) query.getResultList(); } catch (NoResultException e) { return null; } catch (Exception ex) { LOG.error("###WALLET### Error in getListTransaction Service ", ex); throw ex; } return resultSet; }
From source file:org.kitodo.data.database.persistence.BaseDAO.java
License:Open Source License
private void addParameters(Query query, Map<String, Object> parameters) { if (Objects.nonNull(parameters)) { for (Map.Entry<String, Object> parameter : parameters.entrySet()) { if (parameter.getValue() instanceof List) { query.setParameterList(parameter.getKey(), (List) parameter.getValue()); } else { query.setParameter(parameter.getKey(), parameter.getValue()); }/*from ww w . j a v a2 s .co m*/ } } }
From source file:org.openvpms.component.business.dao.hibernate.im.common.Context.java
License:Open Source License
/** * Helper to retrieve the references for a map of objects. This avoids * loading the object if it isn't already present in the session, as long * as the {@link IMObjectDO#getId()} method is the only method invoked. * * @param objects the objects// ww w . jav a 2 s .c o m * @param type the implementation type * @return a map of the object ids to their corresponding references */ public Map<Long, IMObjectReference> getReferences(Map<Long, IMObjectDO> objects, Class<? extends IMObjectDOImpl> type) { List<Long> ids = new ArrayList<>(); Map<Long, IMObjectReference> result = new HashMap<>(); for (Map.Entry<Long, IMObjectDO> entry : objects.entrySet()) { IMObjectDO object = entry.getValue(); if (Hibernate.isInitialized(object)) { result.put(entry.getKey(), object.getObjectReference()); } else { ids.add(entry.getKey()); } } final int size = ids.size(); if (size > 1) { // sort the ids so the references are retrieved in index order Collections.sort(ids); } Query query = session .createQuery("select id, archetypeId, linkId" + " from " + type.getName() + " where id in (:ids)"); query.setParameterList("ids", ids); for (Object match : query.list()) { Object[] values = (Object[]) match; long id = (Long) values[0]; ArchetypeId archId = (ArchetypeId) values[1]; String linkId = (String) values[2]; result.put(id, new IMObjectReference(archId, id, linkId)); } return result; }