Example usage for org.hibernate.criterion Projections property

List of usage examples for org.hibernate.criterion Projections property

Introduction

In this page you can find the example usage for org.hibernate.criterion Projections property.

Prototype

public static PropertyProjection property(String propertyName) 

Source Link

Document

A property value projection

Usage

From source file:cpcc.vvrte.services.db.VvRteRepositoryImpl.java

License:Open Source License

/**
 * {@inheritDoc}//from w w  w  .  j a v a2  s  . c  om
 */
@Override
@SuppressWarnings("unchecked")
public Map<VirtualVehicleState, Integer> getVvStatistics() {
    Map<VirtualVehicleState, Integer> statistics = new HashMap<>();

    statistics.putAll(Stream.of(VirtualVehicleState.values()).map(x -> new SimpleEntry<>(x, Integer.valueOf(0)))
            .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())));

    List<VirtualVehicleState> stateList = session.createCriteria(VirtualVehicle.class)
            .setProjection(Projections.property("state")).list();

    stateList.stream().forEach(x -> statistics.put(x, statistics.get(x) + 1));

    return statistics;
}

From source file:cz.zcu.kiv.eegdatabase.data.dao.SimpleFormLayoutDao.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
public List<String> getFormNames(Person owner) {
    DetachedCriteria criteria = DetachedCriteria.forClass(type);
    criteria.setProjection(Projections.distinct(Projections.property("formName")));
    if (owner != null)
        criteria.add(Restrictions.eq("person.personId", owner.getPersonId()));

    return getHibernateTemplate().findByCriteria(criteria);
}

From source file:cz.zcu.kiv.eegdatabase.data.dao.SimplePersonalLicenseDao.java

License:Apache License

@Override
public List<License> getUsersLicenses(Person person) {
    Criteria criteria = this.getSession().createCriteria(PersonalLicense.class);
    criteria.add(Restrictions.eq("person.personId", person.getPersonId()));
    criteria.setProjection(Projections.property("license"));
    return criteria.list();
}

From source file:data.dao.ColorGroupDao.java

public List<ColorGroup> findDict() {
    Criteria cr = currentSession().createCriteria(getSupportedClass());
    cr.setProjection(Projections.distinct(Projections.property("oldGroupId")));
    return cr.list();
}

From source file:data.dao.FeatureDao.java

private List<Long> getCcoIdDict() {
    Criteria cr = currentSession().createCriteria(getSupportedClass());
    cr.setProjection(Projections.distinct(Projections.property("ccoId")));
    cr.addOrder(Order.asc("ccoId"));
    return cr.list();
}

From source file:data.dao.FeatureDao.java

public List<Long> getCmgDict() {
    Criteria cr = currentSession().createCriteria(getSupportedClass());
    cr.setProjection(Projections.distinct(Projections.property("cmgId")));
    cr.addOrder(Order.asc("cmgId"));
    return cr.list();
}

From source file:database.DataHelper.java

public Object getFieldValue(String field, int id) {
    Object o = null;/*from   w w  w.java 2 s  .  c o m*/
    try {
        session = HibernateUtil.getSessionFactory().openSession();
        o = session.createCriteria(Film.class).setProjection(Projections.property(field))
                .add(Restrictions.eq("idfilm", id)).uniqueResult();
    } catch (Exception e) {
    } finally {
        session.close();
    }

    return o;
}

From source file:de.appsolve.padelcampus.db.dao.generic.BaseEntityDAO.java

@SuppressWarnings("unchecked")
@Override//from   ww w  . ja  v a2 s .c o m
public Page<T> findAllFetchEagerly(Pageable pageable, Set<Criterion> criterions, String... associations) {
    //http://stackoverflow.com/questions/2183617/criteria-api-returns-a-too-small-resultset

    //get the ids of every object that matches the pageable conditions
    //we cannot get the objects directly because we use FetchMode.JOIN which returns the scalar product of all rows in all affected tables
    //and CriteriaSpecification.DISTINCT_ROOT_ENTITY does not work on SQL Level but on in Java after the result is returned from SQL
    Criteria criteria = getPageableCriteria(pageable);
    if (criterions != null) {
        for (Criterion c : criterions) {
            criteria.add(c);
        }
    }
    criteria.setProjection(Projections.distinct(Projections.property("id")));
    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    List<Long> list = criteria.list();

    //once we have the required ids we query for the complete objects
    Criteria objectCriteria = getCriteria();
    for (String association : associations) {
        objectCriteria.setFetchMode(association, FetchMode.JOIN);
    }
    if (!list.isEmpty()) {
        objectCriteria.add(Restrictions.in("id", list));
    }
    objectCriteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    addOrderBy(objectCriteria, pageable);
    List<T> objects = objectCriteria.list();
    sort(objects);

    //we also need the total number of rows
    Criteria rowCountCritieria = getCriteria();
    if (criterions != null) {
        for (Criterion c : criterions) {
            rowCountCritieria.add(c);
        }
    }
    rowCountCritieria.setProjection(Projections.rowCount());
    Long resultCount = (Long) rowCountCritieria.uniqueResult();
    if (resultCount == null) {
        resultCount = objects.size() + 0L;
    }
    Collections.sort(objects);
    PageImpl<T> page = new PageImpl<>(new ArrayList<>(objects), pageable, resultCount);
    return page;
}

From source file:de.csw.expertfinder.persistence.PersistenceStoreFacade.java

License:Open Source License

/**
 * Retrieves the value for the given key from the application data table.
 * //from  w  ww  .jav a 2s  .c o m
 * @param key
 *            the key.
 * @return the value for the given key from the application data table or
 *         null if such value exists.
 */
public String getApplicationData(String key) {
    Session session = sessionFactory.getCurrentSession();
    Criteria criteria = session.createCriteria(ApplicationData.class).add(Restrictions.eq("key", key))
            .setProjection(Projections.property("value"));

    return (String) criteria.uniqueResult();
}

From source file:de.erdesignerng.model.serializer.repository.DictionaryModelSerializer.java

License:Open Source License

/**
 * Get the available repository entries.
 *
 * @param aDialectClass the hibernate dialect class
 * @param aConnection   the jdbc connection
 * @return list of entries/*from  w  ww.  java  2  s  . co  m*/
 * @throws Exception will be thrown in case of an exception
 */
public List<RepositoryEntryDescriptor> getRepositoryEntries(Class aDialectClass, Connection aConnection)
        throws Exception {
    return (List<RepositoryEntryDescriptor>) new HibernateTemplate(aDialectClass, aConnection) {
        @Override
        public Object doInSession(Session aSession) {
            List<RepositoryEntryDescriptor> theResult = new ArrayList<>();

            Criteria theCriteria = aSession.createCriteria(RepositoryEntity.class);
            theCriteria.setProjection(Projections.projectionList().add(Projections.property("id"))
                    .add(Projections.property("name")));
            theCriteria.addOrder(Order.asc("name"));

            for (Object theObject : theCriteria.list()) {
                Object[] theArray = (Object[]) theObject;

                RepositoryEntryDescriptor theEntry = new RepositoryEntryDescriptor();
                theEntry.setId((Long) theArray[0]);
                theEntry.setName((String) theArray[1]);
                theResult.add(theEntry);
            }
            return theResult;
        }
    }.execute();
}