Example usage for org.hibernate.criterion Projections id

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

Introduction

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

Prototype

public static IdentifierProjection id() 

Source Link

Document

An identifier value projection.

Usage

From source file:org.ambraproject.testutils.DummyHibernateDataStore.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
public <T> List<T> getAll(final Class<T> clazz) {
    List ids = hibernateTemplate/*from  w w  w .  jav a 2  s  .c o  m*/
            .findByCriteria(DetachedCriteria.forClass(clazz).setProjection(Projections.id()));
    List<T> results = new ArrayList<T>(ids.size());
    for (Object id : ids) {
        results.add(get(clazz, (Serializable) id));
    }
    return results;
}

From source file:org.ambraproject.trackback.TrackbackServiceImpl.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
public Long createTrackback(String articleDoi, String url, String title, String blogName, String excerpt)
        throws DuplicateTrackbackException {
    if (articleDoi == null) {
        throw new IllegalArgumentException("No DOI specified");
    } else if (url == null || title == null || excerpt == null || blogName == null) {
        throw new IllegalArgumentException("URL, title, excerpt, and blog name must be provided");
    }/*from   w  w  w.  java2  s .c om*/

    Long articleId;
    try {
        articleId = (Long) hibernateTemplate.findByCriteria(DetachedCriteria.forClass(Article.class)
                .add(Restrictions.eq("doi", articleDoi)).setProjection(Projections.id()), 0, 1).get(0);
    } catch (IndexOutOfBoundsException e) {
        throw new IllegalArgumentException("DOI: " + articleDoi + " didn't correspond to an article");
    }

    List<Long> existingTrackbacks = hibernateTemplate.findByCriteria(
            DetachedCriteria.forClass(Trackback.class).add(Restrictions.eq("articleID", articleId))
                    .add(Restrictions.eq("url", url)).setProjection(Projections.id()));
    if (existingTrackbacks.size() > 0) {
        throw new DuplicateTrackbackException(articleDoi, url);
    } else {
        log.debug("Creating trackback for article: {}; url: {}", articleDoi, url);
        Trackback trackback = new Trackback();
        trackback.setArticleID(articleId);
        trackback.setTitle(title);
        trackback.setBlogName(blogName);
        trackback.setUrl(url);
        trackback.setExcerpt(excerpt);
        return (Long) hibernateTemplate.save(trackback);
    }
}

From source file:org.ambraproject.trackback.TrackbackServiceImpl.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
public List<TrackbackView> getTrackbacksForArticle(String articleDoi) {
    if (StringUtils.isEmpty(articleDoi)) {
        throw new IllegalArgumentException("No Doi specified");
    }/* w  w w . java  2  s .co  m*/
    Long articleId;
    String articleTitle;
    try {
        Object[] articleRow = (Object[]) hibernateTemplate.findByCriteria(
                DetachedCriteria.forClass(Article.class).add(Restrictions.eq("doi", articleDoi)).setProjection(
                        Projections.projectionList().add(Projections.id()).add(Projections.property("title"))),
                0, 1).get(0);
        articleId = (Long) articleRow[0];
        articleTitle = (String) articleRow[1];
    } catch (IndexOutOfBoundsException e) {
        throw new IllegalArgumentException("Doi " + articleDoi + " didn't correspond to an article");
    }
    log.debug("loading up trackbacks for article {}", articleDoi);

    List<Trackback> trackbacks = hibernateTemplate.findByCriteria(
            DetachedCriteria.forClass(Trackback.class).add(Restrictions.eq("articleID", articleId))
                    .addOrder(Order.desc("created")).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY));
    List<TrackbackView> results = new ArrayList<TrackbackView>(trackbacks.size());
    for (Trackback trackback : trackbacks) {
        results.add(new TrackbackView(trackback, articleDoi, articleTitle));
    }

    log.info("Loaded {} trackbacks for {}", results.size(), articleDoi);
    return results;
}

From source file:org.ambraproject.trackback.TrackbackServiceImpl.java

License:Apache License

@Override
public int countTrackbacksForArticle(String articleDoi) {
    if (StringUtils.isEmpty(articleDoi)) {
        throw new IllegalArgumentException("Didn't specify an article doi");
    }/*from   www.  java2 s . c  o  m*/
    Long articleId;
    try {
        articleId = (Long) hibernateTemplate.findByCriteria(DetachedCriteria.forClass(Article.class)
                .add(Restrictions.eq("doi", articleDoi)).setProjection(Projections.id()), 0, 1).get(0);
    } catch (IndexOutOfBoundsException e) {
        throw new IllegalArgumentException("Doi: " + articleDoi + " didn't correspond to an article");
    }
    return ((Number) hibernateTemplate.findByCriteria(DetachedCriteria.forClass(Trackback.class)
            .add(Restrictions.eq("articleID", articleId)).setProjection(Projections.rowCount())).get(0))
                    .intValue();
}

From source file:org.balisunrise.daa.hibernate.HQuery.java

License:Open Source License

@Override
public boolean any() {
    return hcrit.setProjection(Projections.id()).uniqueResult() != null;
}

From source file:org.balisunrise.daa.hibernate.HQuery.java

License:Open Source License

@Override
public <V> List<V> ids() {
    return (List<V>) hcrit.setProjection(Projections.id()).list();
}

From source file:org.broadleafcommerce.cms.common.AbstractContentService.java

License:Apache License

private <T> void addSandboxCriteria(SandBox sandbox, Criteria c, Class<T> type, String originalIdProperty) {
    Criterion originalSandboxExpression = Restrictions.eq("originalSandBox", sandbox);
    Criterion currentSandboxExpression = Restrictions.eq("sandbox", sandbox);
    Criterion userSandboxExpression = Restrictions.or(currentSandboxExpression, originalSandboxExpression);
    Criterion productionSandboxExpression = null;
    if (sandbox.getSite() == null || sandbox.getSite().getProductionSandbox() == null) {
        productionSandboxExpression = Restrictions.isNull("sandbox");
    } else {/*from w  ww .j  a va 2 s.co m*/
        productionSandboxExpression = Restrictions.eq("sandbox", sandbox.getSite().getProductionSandbox());
    }

    if (productionSandboxExpression != null) {
        c.add(Restrictions.or(userSandboxExpression, productionSandboxExpression));
    } else {
        c.add(userSandboxExpression);
    }

    // Build a sub-query to exclude items from production that are also in my sandbox.
    // (e.g. my sandbox always wins even if the items in my sandbox don't match the
    // current criteria.)
    //
    // This subquery prevents the following:
    // 1.  Duplicate items (one for sbox, one for prod)
    // 2.  Filter issues where the production item qualifies for the passed in criteria
    //     but has been modified so that the item in the sandbox no longer does.
    // 3.  Inverse of #2.
    DetachedCriteria existsInSboxCriteria = DetachedCriteria.forClass(type, "sboxItem");
    existsInSboxCriteria.add(userSandboxExpression);
    existsInSboxCriteria.add(Restrictions.eq("archivedFlag", false));
    String outerAlias = c.getAlias();
    existsInSboxCriteria.add(Property.forName(outerAlias + ".id").eqProperty("sboxItem." + originalIdProperty));
    existsInSboxCriteria.setProjection(Projections.id());
    c.add(Subqueries.notExists(existsInSboxCriteria));
}

From source file:org.candlepin.model.CertificateSerialCurator.java

License:Open Source License

/**
 * Delete expired serials./* w  w  w . jav  a 2 s. c o m*/
 *
 * @return the number of rows deleted.
 */
public int deleteExpiredSerials() {
    // Some databases don't like to update based on a field that is being updated
    // So we must get expired ids, and then delete them
    @SuppressWarnings("unchecked")
    List<String> ids = this.currentSession().createCriteria(CertificateSerial.class)
            .add(Restrictions.le("expiration", Util.yesterday())).add(getRevokedCriteria())
            .setProjection(Projections.id()).addOrder(Order.asc("id")).list();
    if (ids.isEmpty()) {
        return 0;
    }
    String hql = "DELETE from CertificateSerial " + "WHERE id IN (:expiredids)";
    return this.currentSession().createQuery(hql).setParameterList("expiredids", ids).executeUpdate();
}

From source file:org.candlepin.model.ConsumerCurator.java

License:Open Source License

public boolean isHypervisorIdUsed(String hypervisorId) {
    return currentSession().createCriteria(Consumer.class).createAlias("hypervisorId", "hvsr")
            .add(Restrictions.eq("hvsr.hypervisorId", hypervisorId).ignoreCase())
            .setProjection(Projections.id()).setMaxResults(1).uniqueResult() != null;
}

From source file:org.candlepin.model.ProductCurator.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<String> getProductIdsWithContent(Collection<String> contentIds) {
    if (contentIds == null || contentIds.isEmpty()) {
        return new LinkedList<String>();
    }//from  w w  w.  j  ava2  s. c  o  m
    return currentSession().createCriteria(Product.class).createAlias("productContent", "pcontent")
            .createAlias("pcontent.content", "content").add(Restrictions.in("content.id", contentIds))
            .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).setProjection(Projections.id()).list();
}