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:de.iew.framework.persistence.hibernate.HbmMessageBundleDaoImpl.java

License:Apache License

public List<Locale> getSupportedLocales() {
    Criteria crit = getCurrentSession().createCriteria(TextItem.class).setCacheable(true)
            .setProjection(Projections.distinct(Projections.projectionList()
                    .add(Projections.property("languageCode")).add(Projections.property("countryCode"))));
    crit.setCacheable(true);//from  w w w. j  a  v  a  2 s  .  com
    crit.setResultTransformer(LocaleTupleResultTransformer.DEFAULT);
    return crit.list();
}

From source file:de.phoenix.webresource.TaskResource.java

License:Open Source License

@SuppressWarnings("unchecked")
@Path(PhoenixTask.WEB_RESOURCE_GETALL_TITLES)
@GET//from w w w.jav a 2s.c  o  m
@Produces(MediaType.APPLICATION_JSON)
public Response getAllTitles() {

    Session session = DatabaseManager.getSession();

    try {
        List<String> result = session.createCriteria(Task.class).setProjection(Projections.property("title"))
                .list();

        return Response.ok(result).build();

    } finally {
        if (session != null)
            session.close();
    }
}

From source file:de.powerstaff.business.dao.hibernate.PersonDAOHibernateImpl.java

License:Open Source License

protected Collection<GenericSearchResult> performSearchByContact(final String aContact,
        final ContactType aContactType, final String[] aDisplayProperties, final String[] aOrderByProperties,
        final int aMax) {
    return (Collection<GenericSearchResult>) getHibernateTemplate().execute(new HibernateCallback() {

        public Object doInHibernate(Session aSession) throws SQLException {
            Criteria theCriteria = aSession.createCriteria(getEntityClass());

            ProjectionList theList = Projections.projectionList();
            theList.add(Projections.property("id"));
            for (String theProperty : aDisplayProperties) {
                theList.add(Projections.property(theProperty));
            }//from  w  ww. j  av a  2  s.c o m

            theCriteria.setProjection(theList);

            for (String theProperty : aOrderByProperties) {
                theCriteria.addOrder(Order.asc(theProperty));
            }

            Criteria theContacts = theCriteria.createCriteria("contacts");
            theContacts.add(Restrictions.eq("type", aContactType));
            theContacts.add(Restrictions.ilike("value", "%" + aContact + "%"));

            Collection<GenericSearchResult> theResult = new ArrayList<GenericSearchResult>();

            theCriteria.setMaxResults(aMax);
            for (Iterator it = theCriteria.list().iterator(); it.hasNext();) {
                Object[] theRow = (Object[]) it.next();
                GenericSearchResult theRowObject = new GenericSearchResult();
                theRowObject.put(GenericSearchResult.OBJECT_ID_KEY, theRow[0]);
                for (int i = 0; i < aDisplayProperties.length; i++) {
                    theRowObject.put(aDisplayProperties[i], theRow[i + 1]);
                }
                theResult.add(theRowObject);
            }

            return theResult;

        }

    });
}

From source file:de.powerstaff.business.dao.hibernate.StatistikDAOHibernateImpl.java

License:Open Source License

@Override
public List<KontakthistorieEntry> kontakthistorie(final Date aDatumVon, final Date aDatumBis,
        final User aBenutzer) {
    return (List<KontakthistorieEntry>) getHibernateTemplate().execute(new HibernateCallback() {

        @Override// w  w  w.j  a  v a  2 s  .  c o m
        public Object doInHibernate(Session aSession) throws SQLException {
            List<KontakthistorieEntry> theResult = new ArrayList<KontakthistorieEntry>();

            Conjunction theRestrictions = Restrictions.conjunction();
            if (aDatumVon != null) {
                theRestrictions.add(Restrictions.ge("h.creationDate", aDatumVon));
            }
            if (aDatumBis != null) {
                theRestrictions.add(Restrictions.le("h.creationDate", aDatumBis));
            }
            if (aBenutzer != null) {
                theRestrictions.add(Restrictions.eq("h.creationUserID", aBenutzer.getUsername()));
            }

            // Freiberufler
            Criteria theCriteria = aSession.createCriteria(Freelancer.class, "p");
            theCriteria.createCriteria("history", "h");
            theCriteria.add(theRestrictions);

            ProjectionList theProjections = Projections.projectionList();
            theProjections.add(Projections.property("p.name1"));
            theProjections.add(Projections.property("p.name2"));
            theProjections.add(Projections.property("p.code"));
            theProjections.add(Projections.property("h.creationDate"));
            theProjections.add(Projections.property("h.creationUserID"));
            theProjections.add(Projections.property("h.type"));
            theProjections.add(Projections.property("h.description"));

            theCriteria.setProjection(theProjections);
            for (Object theResultObject : theCriteria.list()) {
                Object[] theResultArray = (Object[]) theResultObject;

                KontakthistorieEntry theEntry = new KontakthistorieEntry();
                theEntry.setName1((String) theResultArray[0]);
                theEntry.setName2((String) theResultArray[1]);
                theEntry.setCode((String) theResultArray[2]);
                Timestamp theTimestamp = (Timestamp) theResultArray[3];
                theEntry.setDatum(new Date(theTimestamp.getTime()));
                theEntry.setUserid((String) theResultArray[4]);
                theEntry.setType((HistoryType) theResultArray[5]);
                theEntry.setDescription((String) theResultArray[6]);

                theResult.add(theEntry);
            }

            // Partner
            theCriteria = aSession.createCriteria(Partner.class, "p");
            theCriteria.createCriteria("history", "h");
            theCriteria.add(theRestrictions);

            theProjections = Projections.projectionList();
            theProjections.add(Projections.property("p.name1"));
            theProjections.add(Projections.property("p.name2"));
            theProjections.add(Projections.property("h.creationDate"));
            theProjections.add(Projections.property("h.creationUserID"));
            theProjections.add(Projections.property("h.type"));
            theProjections.add(Projections.property("h.description"));

            theCriteria.setProjection(theProjections);
            for (Object theResultObject : theCriteria.list()) {
                Object[] theResultArray = (Object[]) theResultObject;

                KontakthistorieEntry theEntry = new KontakthistorieEntry();
                theEntry.setName1((String) theResultArray[0]);
                theEntry.setName2((String) theResultArray[1]);
                Timestamp theTimestamp = (Timestamp) theResultArray[2];
                theEntry.setDatum(new Date(theTimestamp.getTime()));
                theEntry.setUserid((String) theResultArray[3]);
                theEntry.setType((HistoryType) theResultArray[4]);
                theEntry.setDescription((String) theResultArray[5]);

                theResult.add(theEntry);
            }

            // Kunden
            theCriteria = aSession.createCriteria(Customer.class, "p");
            theCriteria.createCriteria("history", "h");
            theCriteria.add(theRestrictions);

            theProjections = Projections.projectionList();
            theProjections.add(Projections.property("p.name1"));
            theProjections.add(Projections.property("p.name2"));
            theProjections.add(Projections.property("h.creationDate"));
            theProjections.add(Projections.property("h.creationUserID"));
            theProjections.add(Projections.property("h.type"));
            theProjections.add(Projections.property("h.description"));

            theCriteria.setProjection(theProjections);
            for (Object theResultObject : theCriteria.list()) {
                Object[] theResultArray = (Object[]) theResultObject;

                KontakthistorieEntry theEntry = new KontakthistorieEntry();
                theEntry.setName1((String) theResultArray[0]);
                theEntry.setName2((String) theResultArray[1]);
                Timestamp theTimestamp = (Timestamp) theResultArray[2];
                theEntry.setDatum(new Date(theTimestamp.getTime()));
                theEntry.setUserid((String) theResultArray[3]);
                theEntry.setType((HistoryType) theResultArray[4]);
                theEntry.setDescription((String) theResultArray[5]);

                theResult.add(theEntry);
            }

            Collections.sort(theResult, new ReverseComparator(new BeanComparator("datum")));

            return theResult;
        }
    });
}

From source file:de.sub.goobi.forms.SearchForm.java

License:Open Source License

/**
 * Initialise drop down list of master piece property titles.
 *///from w ww  . ja  v a 2  s . c om
protected void initMasterpiecePropertyTitles() {
    Session session = Helper.getHibernateSession();
    Criteria crit = session.createCriteria(WorkpieceProperty.class);
    crit.addOrder(Order.asc("titel"));
    crit.setProjection(Projections.distinct(Projections.property("title")));
    this.masterpiecePropertyTitles.add(Helper.getTranslation("notSelected"));
    try {
        @SuppressWarnings("unchecked")
        List<String> results = crit.setFirstResult(0).setMaxResults(Integer.MAX_VALUE).list();
        for (String result : results) {
            this.masterpiecePropertyTitles.add(result);
        }
    } catch (HibernateException hbe) {
        logger.warn("Catched HibernateException. List of master piece property titles could be empty!");
    }
}

From source file:de.sub.goobi.forms.SearchForm.java

License:Open Source License

/**
 * Initialise drop down list of process property titles.
 */// ww w .j av a 2s.  c o  m
protected void initProcessPropertyTitles() {
    Session session = Helper.getHibernateSession();
    Criteria crit = session.createCriteria(ProcessProperty.class);
    crit.addOrder(Order.asc("title"));
    crit.setProjection(Projections.distinct(Projections.property("title")));
    this.processPropertyTitles.add(Helper.getTranslation("notSelected"));
    try {
        @SuppressWarnings("unchecked")
        List<String> results = crit.setFirstResult(0).setMaxResults(Integer.MAX_VALUE).list();
        for (String itstr : results) {
            if (itstr != null) {
                this.processPropertyTitles.add(itstr);
            }
        }
    } catch (HibernateException hbe) {
        logger.warn("Catched HibernateException. List of process property titles could be empty!");
    }
}

From source file:de.sub.goobi.forms.SearchForm.java

License:Open Source License

/**
 * Initialise drop down list of step titles.
 *//*from   w w w .  jav  a 2  s.  c  om*/
protected void initStepTitles() {
    Session session = Helper.getHibernateSession();
    Criteria crit = session.createCriteria(Task.class);
    crit.addOrder(Order.asc("title"));
    crit.setProjection(Projections.distinct(Projections.property("title")));
    this.stepTitles.add(Helper.getTranslation("notSelected"));
    try {
        @SuppressWarnings("unchecked")
        List<String> results = crit.setFirstResult(0).setMaxResults(Integer.MAX_VALUE).list();
        for (String result : results) {
            this.stepTitles.add(result);
        }
    } catch (HibernateException hbe) {
        logger.warn("Catched HibernateException. List of step titles could be empty!");
    }
}

From source file:de.sub.goobi.forms.SearchForm.java

License:Open Source License

/**
 * Initialise drop down list of template property titles.
 *//*  w  w  w .j  a  va 2  s.com*/
protected void initTemplatePropertyTitles() {
    Session session = Helper.getHibernateSession();
    Criteria crit = session.createCriteria(TemplateProperty.class);
    crit.addOrder(Order.asc("title"));
    crit.setProjection(Projections.distinct(Projections.property("title")));
    this.templatePropertyTitles.add(Helper.getTranslation("notSelected"));
    try {
        @SuppressWarnings("unchecked")
        List<String> results = crit.setFirstResult(0).setMaxResults(Integer.MAX_VALUE).list();
        for (String result : results) {
            this.templatePropertyTitles.add(result);
        }
    } catch (HibernateException hbe) {
        logger.warn("Catched HibernateException. List of template property titles could be empty!");
    }
}

From source file:de.tudarmstadt.ukp.lmf.api.UbyStatistics.java

License:Apache License

/**
 * Counts the number of {@link SenseAxis} instances between two {@link Lexicon} instances
 * identified by their name. The counted sense axes are filtered by the
 * specified type.<p>/*from w w  w  .  j  a v a  2 s . c  o m*/
 * <b>Important properties of this method:</b>
 * <ul>
 *       <li>Only alignments between {@link Sense} instances are considered.</li>
 *       <li>The sources of the alignments are not distinguished.</li>
 *       <li>The lexicons are identified by identifier prefixes of the aligned senses.</li>
 * </ul>
 *       
 * @param type
 *          Type of sense axes to be considered when counting
 * 
 * @param lex1Name
 *          The name of the first of two lexicons between which sense axes should be counted
 * 
 * @param lex2Name
 *          The name of the second of two lexicons between which sense axes should be counted
 * 
 * @return the number of sense axes between the lexicons filtered by the specified sense axes type.
 * This method returns zero if a lexicon with the specified name does not exist or one of the
 * consumed arguments is null.
 * 
 * @see ESenseAxisType
 */
public long countSenseAxesPerLexiconPair(ESenseAxisType type, String lex1Name, String lex2Name) {

    // get prefix for res1Name
    Criteria c1 = session.createCriteria(Sense.class, "s");
    c1 = c1.createCriteria("lexicalEntry");
    c1 = c1.createCriteria("lexicon");
    c1 = c1.add(Restrictions.eq("name", lex1Name));
    c1 = c1.setProjection(Projections.property("s.id"));
    c1 = c1.setMaxResults(1);
    String res1 = (String) c1.uniqueResult();
    //get prefix for res2Name
    Criteria c2 = session.createCriteria(Sense.class, "s");
    c2 = c2.createCriteria("lexicalEntry");
    c2 = c2.createCriteria("lexicon");
    c2 = c2.add(Restrictions.eq("name", lex2Name));
    c2 = c2.setProjection(Projections.property("s.id"));
    c2 = c2.setMaxResults(1);
    String res2 = (String) c2.uniqueResult();
    String pref1 = "";
    String pref2 = "";
    if (res1 != null && res2 != null) {
        pref1 = res1.split("_")[0];
        if (res1.split("_")[1].equals("en") || res1.split("_")[1].equals("de")) {
            pref1 += "_" + res1.split("_")[1];
        }
        pref2 = res2.split("_")[0];
        if (res2.split("_")[1].equals("en") || res2.split("_")[1].equals("de")) {
            pref2 += "_" + res2.split("_")[1];
        }
        // get alignments with these prefixes
        Criteria criteria = session.createCriteria(SenseAxis.class);
        criteria = criteria.add(Restrictions.eq("senseAxisType", type));
        criteria = criteria.add(Restrictions.like("senseOne.id", pref1, MatchMode.START));
        criteria = criteria.add(Restrictions.like("senseTwo.id", pref2, MatchMode.START));
        criteria = criteria.setProjection(Projections.rowCount());
        return (Long) criteria.uniqueResult();
    } else {
        return 0L;
    }
}

From source file:de.tudarmstadt.ukp.lmf.api.UbyStatistics.java

License:Apache License

/**
 * Returns a {@link List} containing all {@link SenseAxis} instances between two {@link Lexicon} instances
 * identified by their name. The counted sense axes are filtered by the
 * specified type.<p>//from   ww w  .ja v a 2  s .c  o m
 * <b>Important properties of this method:</b>
 * <ul>
 *       <li>Only alignments between {@link Sense} instances are considered.</li>
 *       <li>The sources of the alignments are not distinguished.</li>
 *       <li>The lexicons are identified by identifier prefixes of the aligned senses.</li>
 * </ul>
 *       
 * @param type
 *          Type of sense axes to be returned
 * 
 * @param lex1Name
 *          The name of the first of two lexicons between which sense axes should be found
 * @param lex2Name
 *          The name of the second of two lexicons between which sense axes should be found
 * 
 * @return the list of sense axes between the lexicons filtered by the specified sense axes type.
 * This method returns an empty list if a lexicon with the specified name does not exist or one of
 * the consumed arguments is null.
 * 
 * @see ESenseAxisType
 */
@SuppressWarnings("unchecked")
public List<SenseAxis> getSenseAxesPerLexiconPair(ESenseAxisType type, String lex1Name, String lex2Name) {
    // get prefix for res1Name
    Criteria c1 = session.createCriteria(Sense.class, "s");
    c1 = c1.createCriteria("lexicalEntry");
    c1 = c1.createCriteria("lexicon");
    c1 = c1.add(Restrictions.eq("name", lex1Name));
    c1 = c1.setProjection(Projections.property("s.id"));
    c1 = c1.setMaxResults(1);
    String res1 = (String) c1.uniqueResult();
    //get prefix for res2Name
    Criteria c2 = session.createCriteria(Sense.class, "s");
    c2 = c2.createCriteria("lexicalEntry");
    c2 = c2.createCriteria("lexicon");
    c2 = c2.add(Restrictions.eq("name", lex2Name));
    c2 = c2.setProjection(Projections.property("s.id"));
    c2 = c2.setMaxResults(1);
    String res2 = (String) c2.uniqueResult();
    String pref1 = "";
    String pref2 = "";
    if (res1 != null && res2 != null) {
        pref1 = res1.split("_")[0];
        if (res1.split("_")[1].equals("en") || res1.split("_")[1].equals("de")) {
            pref1 += "_" + res1.split("_")[1];
        }
        pref2 = res2.split("_")[0];
        if (res2.split("_")[1].equals("en") || res2.split("_")[1].equals("de")) {
            pref2 += "_" + res2.split("_")[1];
        }
        // get alignments with these prefixes
        Criteria criteria = session.createCriteria(SenseAxis.class);
        criteria = criteria.add(Restrictions.eq("senseAxisType", type));
        criteria = criteria.add(Restrictions.like("senseOne.id", pref1, MatchMode.START));
        criteria = criteria.add(Restrictions.like("senseTwo.id", pref2, MatchMode.START));

        return criteria.list();
    } else
        return new ArrayList<SenseAxis>();
}