List of usage examples for org.hibernate Query setResultTransformer
@Deprecated Query<R> setResultTransformer(ResultTransformer transformer);
From source file:com.eryansky.common.orm.core.hibernate.support.BasicHibernateDao.java
License:Apache License
/** * Querydistinct transformer,????distinct? * //from w w w . j a va 2s. c o m * @param queryOrNamedQuery hql HibernateNamedQuery * @param values * * @return List */ public <X> List<X> distinct(String queryOrNamedQuery, Map<String, Object> values) { Query query = createQuery(queryOrNamedQuery, values); query.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY); List<X> result = query.list(); if (CollectionUtils.isEmpty(result) || result.get(0) == null) { return Lists.newArrayList(); } return result; }
From source file:com.evolveum.midpoint.repo.sql.helpers.NameResolutionHelper.java
License:Apache License
public <C extends Containerable> void resolveNamesIfRequested(Session session, PrismContainerValue<C> containerValue, Collection<SelectorOptions<GetOperationOptions>> options) { GetOperationOptions rootOptions = SelectorOptions.findRootOptions(options); if (GetOperationOptions.isResolveNames(rootOptions)) { final List<String> oidsToResolve = new ArrayList<>(); Visitor oidExtractor = new Visitor() { @Override//from w ww . ja va 2s.c om public void visit(Visitable visitable) { if (visitable instanceof PrismReferenceValue) { PrismReferenceValue value = (PrismReferenceValue) visitable; if (value.getTargetName() != null) { // just for sure return; } if (value.getObject() != null) { // improbable but possible value.setTargetName(value.getObject().getName()); return; } if (value.getOid() == null) { // shouldn't occur as well return; } oidsToResolve.add(value.getOid()); } } }; containerValue.accept(oidExtractor); if (!oidsToResolve.isEmpty()) { Query query = session.getNamedQuery("resolveReferences"); query.setParameterList("oid", oidsToResolve); query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP); List<Map<String, Object>> results = query.list(); final Map<String, PolyString> oidNameMap = consolidateResults(results); Visitor nameSetter = new Visitor() { @Override public void visit(Visitable visitable) { if (visitable instanceof PrismReferenceValue) { PrismReferenceValue value = (PrismReferenceValue) visitable; if (value.getTargetName() == null && value.getOid() != null) { value.setTargetName(oidNameMap.get(value.getOid())); } } } }; containerValue.accept(nameSetter); } } }
From source file:com.evolveum.midpoint.repo.sql.helpers.ObjectRetriever.java
License:Apache License
public <T extends ObjectType> PrismObject<T> getObjectInternal(Session session, Class<T> type, String oid, Collection<SelectorOptions<GetOperationOptions>> options, boolean lockForUpdate, OperationResult operationResult) throws ObjectNotFoundException, SchemaException, DtoTranslationException { boolean lockedForUpdateViaHibernate = false; boolean lockedForUpdateViaSql = false; LockOptions lockOptions = new LockOptions(); //todo fix lock for update!!!!! if (lockForUpdate) { if (getConfiguration().isLockForUpdateViaHibernate()) { lockOptions.setLockMode(LockMode.PESSIMISTIC_WRITE); lockedForUpdateViaHibernate = true; } else if (getConfiguration().isLockForUpdateViaSql()) { LOGGER.trace("Trying to lock object {} for update (via SQL)", oid); long time = System.currentTimeMillis(); SQLQuery q = session.createSQLQuery("select oid from m_object where oid = ? for update"); q.setString(0, oid);/* w w w. ja va 2 s. c o m*/ Object result = q.uniqueResult(); if (result == null) { return throwObjectNotFoundException(type, oid); } if (LOGGER.isTraceEnabled()) { LOGGER.trace("Locked via SQL (in {} ms)", System.currentTimeMillis() - time); } lockedForUpdateViaSql = true; } } if (LOGGER.isTraceEnabled()) { if (lockedForUpdateViaHibernate) { LOGGER.trace("Getting object {} with locking for update (via hibernate)", oid); } else if (lockedForUpdateViaSql) { LOGGER.trace("Getting object {}, already locked for update (via SQL)", oid); } else { LOGGER.trace("Getting object {} without locking for update", oid); } } GetObjectResult fullObject = null; if (!lockForUpdate) { Query query = session.getNamedQuery("get.object"); query.setString("oid", oid); query.setResultTransformer(GetObjectResult.RESULT_TRANSFORMER); query.setLockOptions(lockOptions); fullObject = (GetObjectResult) query.uniqueResult(); } else { // we're doing update after this get, therefore we load full object right now // (it would be loaded during merge anyway) // this just loads object to hibernate session, probably will be removed later. Merge after this get // will be faster. Read and use object only from fullObject column. // todo remove this later [lazyman] Criteria criteria = session.createCriteria(ClassMapper.getHQLTypeClass(type)); criteria.add(Restrictions.eq("oid", oid)); criteria.setLockMode(lockOptions.getLockMode()); RObject obj = (RObject) criteria.uniqueResult(); if (obj != null) { obj.toJAXB(prismContext, null).asPrismObject(); fullObject = new GetObjectResult(obj.getFullObject(), obj.getStringsCount(), obj.getLongsCount(), obj.getDatesCount(), obj.getReferencesCount(), obj.getPolysCount(), obj.getBooleansCount()); } } LOGGER.trace("Got it."); if (fullObject == null) { throwObjectNotFoundException(type, oid); } LOGGER.trace("Transforming data to JAXB type."); PrismObject<T> prismObject = updateLoadedObject(fullObject, type, oid, options, session, operationResult); validateObjectType(prismObject, type); // this was implemented to allow report parsing errors as warnings to upper layers; // however, it causes problems when serialization problems are encountered: in such cases, we put // FATAL_ERROR to the result here, and it should be then removed or muted (which is a complication) // -- so, as the parsing errors are not implemented, we disabled this code as well // subResult.computeStatusIfUnknown(); // if (subResult.isWarning() || subResult.isError() || subResult.isInProgress()) { // prismObject.asObjectable().setFetchResult(subResult.createOperationResultType()); // } return prismObject; }
From source file:com.evolveum.midpoint.repo.sql.helpers.ObjectRetriever.java
License:Apache License
public <F extends FocusType> PrismObject<F> searchShadowOwnerAttempt(String shadowOid, Collection<SelectorOptions<GetOperationOptions>> options, OperationResult result) { LOGGER_PERFORMANCE.debug("> search shadow owner for oid={}", shadowOid); PrismObject<F> owner = null;//from w w w .j av a2s. c o m Session session = null; try { session = baseHelper.beginReadOnlyTransaction(); LOGGER.trace("Selecting account shadow owner for account {}.", new Object[] { shadowOid }); Query query = session.getNamedQuery("searchShadowOwner.getOwner"); query.setString("oid", shadowOid); query.setResultTransformer(GetObjectResult.RESULT_TRANSFORMER); List<GetObjectResult> focuses = query.list(); LOGGER.trace("Found {} focuses, transforming data to JAXB types.", new Object[] { (focuses != null ? focuses.size() : 0) }); if (focuses == null || focuses.isEmpty()) { // account shadow owner was not found return null; } if (focuses.size() > 1) { LOGGER.warn("Found {} owners for shadow oid {}, returning first owner.", new Object[] { focuses.size(), shadowOid }); } GetObjectResult focus = focuses.get(0); owner = updateLoadedObject(focus, (Class<F>) FocusType.class, null, options, session, result); session.getTransaction().commit(); } catch (SchemaException | RuntimeException ex) { baseHelper.handleGeneralException(ex, session, result); } finally { baseHelper.cleanupSessionAndResult(session, result); } return owner; }
From source file:com.evolveum.midpoint.repo.sql.helpers.ObjectRetriever.java
License:Apache License
public PrismObject<UserType> listAccountShadowOwnerAttempt(String accountOid, OperationResult result) throws ObjectNotFoundException { LOGGER_PERFORMANCE.debug("> list account shadow owner oid={}", accountOid); PrismObject<UserType> userType = null; Session session = null;/*from ww w . j av a2s. c o m*/ try { session = baseHelper.beginReadOnlyTransaction(); Query query = session.getNamedQuery("listAccountShadowOwner.getUser"); query.setString("oid", accountOid); query.setResultTransformer(GetObjectResult.RESULT_TRANSFORMER); List<GetObjectResult> users = query.list(); LOGGER.trace("Found {} users, transforming data to JAXB types.", new Object[] { (users != null ? users.size() : 0) }); if (users == null || users.isEmpty()) { // account shadow owner was not found return null; } if (users.size() > 1) { LOGGER.warn("Found {} users for account oid {}, returning first user. [interface change needed]", new Object[] { users.size(), accountOid }); } GetObjectResult user = users.get(0); userType = updateLoadedObject(user, UserType.class, null, null, session, result); session.getTransaction().commit(); } catch (SchemaException | RuntimeException ex) { baseHelper.handleGeneralException(ex, session, result); } finally { baseHelper.cleanupSessionAndResult(session, result); } return userType; }
From source file:com.evolveum.midpoint.repo.sql.helpers.ObjectRetriever.java
License:Apache License
public <T extends ShadowType> List<PrismObject<T>> listResourceObjectShadowsAttempt(String resourceOid, Class<T> resourceObjectShadowType, OperationResult result) throws ObjectNotFoundException, SchemaException { LOGGER_PERFORMANCE.debug("> list resource object shadows {}, for resource oid={}", new Object[] { resourceObjectShadowType.getSimpleName(), resourceOid }); List<PrismObject<T>> list = new ArrayList<>(); Session session = null;//w ww. ja v a 2 s .c o m try { session = baseHelper.beginReadOnlyTransaction(); Query query = session.getNamedQuery("listResourceObjectShadows"); query.setString("oid", resourceOid); query.setResultTransformer(GetObjectResult.RESULT_TRANSFORMER); List<GetObjectResult> shadows = query.list(); LOGGER.debug("Query returned {} shadows, transforming to JAXB types.", new Object[] { (shadows != null ? shadows.size() : 0) }); if (shadows != null) { for (GetObjectResult shadow : shadows) { PrismObject<T> prismObject = updateLoadedObject(shadow, resourceObjectShadowType, null, null, session, result); list.add(prismObject); } } session.getTransaction().commit(); } catch (SchemaException | RuntimeException ex) { baseHelper.handleGeneralException(ex, session, result); } finally { baseHelper.cleanupSessionAndResult(session, result); } return list; }
From source file:com.evolveum.midpoint.repo.sql.query.custom.OrgFilterQuery.java
License:Apache License
@Override public RQuery createQuery(ObjectQuery objectQuery, Class<? extends ObjectType> type, Collection<SelectorOptions<GetOperationOptions>> options, boolean countingObjects, Session session) { OrgFilter filter = (OrgFilter) objectQuery.getFilter(); LOGGER.trace("createOrgQuery {}, counting={}, filter={}", new Object[] { type.getSimpleName(), countingObjects, filter }); if (countingObjects) { return countQuery(filter, type, session); }/*from w w w .j a v a 2 s .c o m*/ StringBuilder sb = new StringBuilder(); if (OrgFilter.Scope.ONE_LEVEL.equals(filter.getScope())) { sb.append( "select o.fullObject,o.stringsCount,o.longsCount,o.datesCount,o.referencesCount,o.polysCount,o.booleansCount from "); sb.append(ClassMapper.getHQLType(type)).append( " as o where o.oid in (select distinct p.ownerOid from RObjectReference p where p.targetOid=:oid and p.referenceType=0)"); } else { sb.append( "select o.fullObject,o.stringsCount,o.longsCount,o.datesCount,o.referencesCount,o.polysCount,o.booleansCount from "); sb.append(ClassMapper.getHQLType(type)).append(" as o where o.oid in ("); //todo change to sb.append("select d.descendantOid from ROrgClosure as d where d.ancestorOid = :oid and d.descendantOid != :oid)"); sb.append( "select distinct d.descendantOid from ROrgClosure as d where d.ancestorOid = :oid and d.descendantOid != :oid)"); } Query query = session.createQuery(sb.toString()); query.setString("oid", filter.getOrgRef().getOid()); query.setResultTransformer(GetObjectResult.RESULT_TRANSFORMER); return new RQueryImpl(query); }
From source file:com.evolveum.midpoint.repo.sql.query.OrgFilterQuery.java
License:Apache License
@Override public RQuery createQuery(ObjectQuery objectQuery, Class<? extends ObjectType> type, Collection<SelectorOptions<GetOperationOptions>> options, boolean countingObjects, Session session) { OrgFilter filter = (OrgFilter) objectQuery.getFilter(); LOGGER.trace("createOrgQuery {}, counting={}, filter={}", new Object[] { type.getSimpleName(), countingObjects, filter }); if (countingObjects) { return countQuery(filter, type, session); }/*from www.j a v a 2 s. c o m*/ StringBuilder sb = new StringBuilder(); if (OrgFilter.Scope.ONE_LEVEL.equals(filter.getScope())) { sb.append( "select o.fullObject,o.stringsCount,o.longsCount,o.datesCount,o.referencesCount,o.polysCount from "); sb.append(ClassMapper.getHQLType(type)).append( " as o where o.oid in (select distinct p.ownerOid from RParentOrgRef p where p.targetOid=:oid)"); } else { sb.append( "select o.fullObject,o.stringsCount,o.longsCount,o.datesCount,o.referencesCount,o.polysCount from "); sb.append(ClassMapper.getHQLType(type)).append(" as o where o.oid in ("); //todo change to sb.append("select d.descendantOid from ROrgClosure as d where d.ancestorOid = :oid and d.descendantOid != :oid)"); sb.append( "select distinct d.descendantOid from ROrgClosure as d where d.ancestorOid = :oid and d.descendantOid != :oid)"); } Query query = session.createQuery(sb.toString()); query.setString("oid", filter.getOrgRef().getOid()); query.setResultTransformer(GetObjectResult.RESULT_TRANSFORMER); return new RQueryImpl(query); }
From source file:com.evolveum.midpoint.repo.sql.query2.hqm.RootHibernateQuery.java
License:Apache License
public Query getAsHqlQuery(Session session) { String text = getAsHqlText(0); LOGGER.trace("HQL text generated:\n{}", text); Query query = session.createQuery(text); for (Map.Entry<String, QueryParameterValue> parameter : parameters.entrySet()) { String name = parameter.getKey(); QueryParameterValue parameterValue = parameter.getValue(); LOGGER.trace("Parameter {} = {}", name, parameterValue.debugDump()); if (parameterValue.getValue() instanceof Collection) { if (parameterValue.getType() != null) { query.setParameterList(name, (Collection) parameterValue.getValue(), parameterValue.getType()); } else { query.setParameterList(name, (Collection) parameterValue.getValue()); }/*from w w w . ja v a2 s . co m*/ } else { if (parameterValue.getType() != null) { query.setParameter(name, parameterValue.getValue(), parameterValue.getType()); } else { query.setParameter(name, parameterValue.getValue()); } } } if (maxResults != null) { query.setMaxResults(maxResults); } if (firstResult != null) { query.setFirstResult(firstResult); } if (resultTransformer != null) { query.setResultTransformer(resultTransformer); } return query; }
From source file:com.evolveum.midpoint.repo.sql.SqlRepositoryServiceImpl.java
License:Apache License
private <T extends ObjectType> PrismObject<T> getObject(Session session, Class<T> type, String oid, Collection<SelectorOptions<GetOperationOptions>> options, boolean lockForUpdate) throws ObjectNotFoundException, SchemaException, DtoTranslationException, QueryException { boolean lockedForUpdateViaHibernate = false; boolean lockedForUpdateViaSql = false; LockOptions lockOptions = new LockOptions(); //todo fix lock for update!!!!! if (lockForUpdate) { if (getConfiguration().isLockForUpdateViaHibernate()) { lockOptions.setLockMode(LockMode.PESSIMISTIC_WRITE); lockedForUpdateViaHibernate = true; } else if (getConfiguration().isLockForUpdateViaSql()) { if (LOGGER.isTraceEnabled()) { LOGGER.trace("Trying to lock object " + oid + " for update (via SQL)"); }// ww w . j ava2 s . c o m long time = System.currentTimeMillis(); SQLQuery q = session.createSQLQuery("select oid from m_object where oid = ? for update"); q.setString(0, oid); Object result = q.uniqueResult(); if (result == null) { return throwObjectNotFoundException(type, oid); } if (LOGGER.isTraceEnabled()) { LOGGER.trace("Locked via SQL (in " + (System.currentTimeMillis() - time) + " ms)"); } lockedForUpdateViaSql = true; } } if (LOGGER.isTraceEnabled()) { if (lockedForUpdateViaHibernate) { LOGGER.trace("Getting object " + oid + " with locking for update (via hibernate)"); } else if (lockedForUpdateViaSql) { LOGGER.trace("Getting object " + oid + ", already locked for update (via SQL)"); } else { LOGGER.trace("Getting object " + oid + " without locking for update"); } } GetObjectResult fullObject = null; if (!lockForUpdate) { Query query = session.getNamedQuery("get.object"); query.setString("oid", oid); query.setResultTransformer(GetObjectResult.RESULT_TRANSFORMER); query.setLockOptions(lockOptions); fullObject = (GetObjectResult) query.uniqueResult(); } else { // we're doing update after this get, therefore we load full object right now // (it would be loaded during merge anyway) // this just loads object to hibernate session, probably will be removed later. Merge after this get // will be faster. Read and use object only from fullObject column. // todo remove this later [lazyman] Criteria criteria = session.createCriteria(ClassMapper.getHQLTypeClass(type)); criteria.add(Restrictions.eq("oid", oid)); criteria.setLockMode(lockOptions.getLockMode()); RObject obj = (RObject) criteria.uniqueResult(); if (obj != null) { obj.toJAXB(getPrismContext(), null).asPrismObject(); fullObject = new GetObjectResult(obj.getFullObject(), obj.getStringsCount(), obj.getLongsCount(), obj.getDatesCount(), obj.getReferencesCount(), obj.getPolysCount()); } } LOGGER.trace("Got it."); if (fullObject == null) { throwObjectNotFoundException(type, oid); } LOGGER.trace("Transforming data to JAXB type."); PrismObject<T> prismObject = updateLoadedObject(fullObject, type, options, session); validateObjectType(prismObject, type); return prismObject; }