/** * Transform to bean. See bug * http://opensource.atlassian.com/projects/hibernate/browse/HHH-2463 * * @param aliasList * the alias list * @param resultList * the result list * * @return the list of GisFeatureDistance */ //TODO tests zip test public static List<GisFeatureDistance> transformToGisFeatureDistance(String aliasList[], List<?> resultList, Map<Long, Set<String>> featureIdToZipCodesMap,Class clazz) { List<GisFeatureDistance> results = new ArrayList<GisFeatureDistance>(); if (aliasList != null && !resultList.isEmpty()) { ResultTransformer tr = new AliasToBeanResultTransformer(GisFeatureDistance.class); Iterator<?> it = resultList.iterator(); Object[] obj; GisFeatureDistance gisFeatureDistance; while (it.hasNext()) { obj = (Object[]) it.next(); gisFeatureDistance = (GisFeatureDistance) tr.transformTuple(obj, aliasList); int indexInList = results.indexOf(gisFeatureDistance); if (indexInList == -1) { gisFeatureDistanceFactory.updateFields(gisFeatureDistance,clazz); results.add(gisFeatureDistance); if (featureIdToZipCodesMap != null){ gisFeatureDistance.setZipCodes(featureIdToZipCodesMap.get(gisFeatureDistance.getId())); } } } } return results; }
/** * Transform to bean. See bug * http://opensource.atlassian.com/projects/hibernate/browse/HHH-2463 * * @param aliasList * the alias list * @param resultList * the result list * * @return the list of {@link StreetDistance} */ public static List<StreetDistance> transformToStreetDistance(String aliasList[], List<?> resultList) { List<StreetDistance> transformList = new ArrayList<StreetDistance>(); if (aliasList != null && !resultList.isEmpty()) { AliasToBeanResultTransformer tr = new AliasToBeanResultTransformer(StreetDistance.class); Iterator<?> it = resultList.iterator(); Object[] obj; while (it.hasNext()) { obj = (Object[]) it.next(); StreetDistance streetDistance = (StreetDistance) tr.transformTuple(obj, aliasList); streetDistance.updateFields(); transformList.add(streetDistance); } } return transformList; }
private PostWithXminAndXmax getPost(EntityManager entityManager, Integer id) { List<PostWithXminAndXmax> result = (List<PostWithXminAndXmax>) entityManager.createNativeQuery( "SELECT " + " id, title, CAST(xmin AS text), CAST(xmax AS text) " + "FROM Post " + "WHERE id = :id") .setParameter("id", id) .unwrap(Query.class) .setResultTransformer(new AliasToBeanResultTransformer(PostWithXminAndXmax.class)) .getResultList(); return !result.isEmpty() ? result.get(0) : null; }
@Override public ArrayList<EntidadAbstracta> filtrarPorTexto(String text) { AbstractHibernateDAO dummy = new AbstractHibernateDAO(){}; ArrayList<FamiliaEquipo> listaResultado = new ArrayList<FamiliaEquipo>(); try { dummy.iniciaOperacion(); Query query = dummy.getSession().createSQLQuery( "SELECT distinct f.* , " + "(select valor from precio_historico " + "where familia_id = f.id and tipo_id = 1 and fechaBaja is null " + "order by id desc limit 1) as valorPosesion, " + "(select valor from precio_historico " + "where familia_id = f.id and tipo_id = 2 and fechaBaja is null " + "order by id desc limit 1) as valorUtilizacion FROM familia_equipo f " + "left join precio_historico ph on ph.familia_id = f.id " + "where f.nombre like '%"+ text +"%' ").setResultTransformer(new AliasToBeanResultTransformer(FamiliaEquipoBean.class)); for(FamiliaEquipoBean a: (List<FamiliaEquipoBean>)query.list()) { listaResultado.add(a.convert2FamiliaEquipo()); } } catch (HibernateException he) { dummy.manejaExcepcion(he); } finally { dummy.terminaOperacion(); } ArrayList<EntidadAbstracta> list1 = new ArrayList<EntidadAbstracta>(listaResultado); return list1; }
/** {@inheritDoc} */ @Override @SuppressWarnings("unchecked") public List<OrganizationPaginationBean> findByStrCodeUp(String strCodeUp) throws ServiceException { if (StringUtils.isEmpty(strCodeUp)) { throw new IllegalArgumentException("The organization's code must not be null or empty"); } final Session session = HibernateSessionManager.getSession(); Transaction tx = null; List<OrganizationPaginationBean> organizations = new ArrayList<OrganizationPaginationBean>(); try { tx = session.beginTransaction(); Criteria criteria = repository.getCriteria(session, Organization.class) .setProjection(Projections.projectionList() .add( Projections.property("strCode").as("strCode")) .add( Projections.property("strNameR").as("strNameR")) .add( Projections.property("comName").as("comName"))) .add(Restrictions.eq("strCodeUp", strCodeUp)) .add(Restrictions.eq("delKbn", Constants.STATUS_ACTIVE)) .addOrder(Order.asc("strCode")) .setResultTransformer(new AliasToBeanResultTransformer(OrganizationPaginationBean.class)); organizations = criteria.list(); // Release transaction tx.commit(); if (CollectionUtils.isEmpty(organizations)) { throw new ObjectNotFoundException("Could not find any Organization with strCodeUp " + strCodeUp); } } catch (HibernateException ex) { if (tx != null) { tx.rollback(); } throw new ServiceException("An exception occured while finding Organization list with strCodeUp " + strCodeUp, ex); } finally { HibernateSessionManager.closeSession(session); } return organizations; }
/** * @param request * @return * @see edu.utah.further.core.chain.AbstractRequestHandler#process(edu.utah.further.core.api.chain.ChainRequest) * @see http://opensource.atlassian.com/projects/hibernate/browse/HHH-817 */ @Override public boolean process(final ChainRequest request) { final HibernateExecReq executionReq = new HibernateExecReq(request); // Validate required input final GenericCriteria hibernateCriteria = executionReq.getResult(); notNull(hibernateCriteria, "Expected Hibernate criteria"); final Class<? extends PersistentEntity<?>> domainClass = executionReq .getRootEntity(); final Class<? extends PersistentEntity<?>> entityClass = dao .getEntityClass(domainClass); notNull(entityClass, "Expected root entity class"); final SessionFactory sessionFactory = executionReq.getSessionFactory(); notNull(sessionFactory, "Expected SessionFactory"); final ClassMetadata classMetadata = sessionFactory.getClassMetadata(entityClass); final String identifierName = classMetadata.getIdentifierPropertyName(); final Type identifierType = classMetadata.getIdentifierType(); // A hack to obtain projections out of the critieria by casting to the Hibernate // implementation. TODO: improve adapter to do that via interface access final ProjectionList projectionList = Projections.projectionList(); final Projection existingProjection = ((CriteriaImpl) hibernateCriteria .getHibernateCriteria()).getProjection(); if (existingProjection != null && !overrideExistingProjection) { return false; } if (identifierType.isComponentType()) { final ComponentType componentType = (ComponentType) identifierType; final String[] idPropertyNames = componentType.getPropertyNames(); // Add distinct to the first property projectionList.add( Projections.distinct(Property.forName(identifierName + PROPERTY_SCOPE_CHAR + idPropertyNames[0])), idPropertyNames[0]); // Add the remaining properties to the projection list for (int i = 1; i < idPropertyNames.length; i++) { projectionList.add( Property.forName(identifierName + PROPERTY_SCOPE_CHAR + idPropertyNames[i]), idPropertyNames[i]); } hibernateCriteria.setProjection(projectionList); hibernateCriteria.setResultTransformer(new AliasToBeanResultTransformer( ReflectionUtils.findField(entityClass, identifierName).getType())); } else { // 'this' required to avoid HHH-817 projectionList.add(Projections.distinct(Property.forName(THIS_CONTEXT + identifierName))); hibernateCriteria.setProjection(projectionList); } executionReq.setResult(hibernateCriteria); return false; }
/** * Queries the database for a result set which consists of only a single property value for each of all rows that match the criteria within the table. * * @param model the model class which represents the table * @param criteria the criteria * @param propertyType the type of the property * @param property the property name * @param <T> the type of the model which represents the table * @param <V> the type of the property value within the result set * @return a list of values for the specified property */ public <T, V> List<V> propertyFind(Class<T> model, Criteria criteria, Class<V> propertyType, String property) { if (model == null || propertyType == null || property == null) throw new IllegalArgumentException(); org.hibernate.Criteria c = session.createCriteria(model); if (criteria != null) criteria.accept(c); c.setProjection(Projections.property(property)); c.setResultTransformer(new AliasToBeanResultTransformer(propertyType)); return c.list(); }