Ejemplo n.º 1
0
  /**
   * Returns true if a persisted record exsits for the given id.
   *
   * @param id primary key of entity
   * @return true if entity exists for id, false if entity does not exist
   */
  public boolean isIdPersisted(Serializable id) {
    Criteria criteria =
        getSession()
            .createCriteria(getPersistentClass())
            .add(Restrictions.idEq(id))
            .setProjection(Projections.rowCount());

    return (criteria.uniqueResult() != null && ((Long) criteria.uniqueResult()) > 0);
  }
 @Override
 public <T extends GettableById> Number count(Class<T> currentClass, Criterion... restrictions) {
   validateTransaction();
   Criteria criteria = getSession().createCriteria(currentClass);
   criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
   criteria.setProjection(Projections.rowCount());
   for (Criterion restriction : restrictions) criteria.add(restriction);
   return ((Number) criteria.uniqueResult());
 }
Ejemplo n.º 3
0
 @SuppressWarnings("unchecked")
 protected T findByCriteriaSingle(Criterion... criterion) {
   Criteria crit = getSession().createCriteria(getPersistentClass());
   for (Criterion c : criterion) {
     crit.add(c);
   }
   crit.setCacheable(queriesCached);
   return (T) crit.uniqueResult();
 }
Ejemplo n.º 4
0
 @SuppressWarnings("unchecked")
 public T findByExampleSingle(T exampleInstance, String... excludeProperty) {
   Criteria crit = getSession().createCriteria(getPersistentClass());
   Example example = Example.create(exampleInstance);
   for (String exclude : excludeProperty) {
     example.excludeProperty(exclude);
   }
   crit.add(example);
   crit.setCacheable(queriesCached);
   return (T) crit.uniqueResult();
 }
Ejemplo n.º 5
0
 public Teacher findByName(String name) {
   Criteria criteria;
   Transaction tx;
   Teacher teacher;
   Session session = ProductionSessionFactory.getDefault().getCurrentSession();
   tx = session.beginTransaction();
   try {
     criteria = session.createCriteria(persistentClass).add(Restrictions.eq("name", name));
     teacher = (Teacher) criteria.uniqueResult();
   } finally {
     tx.commit();
   }
   return teacher;
 }
Ejemplo n.º 6
0
  public String getLastCommit(String repositoryId, String branchRef) {
    LOG.debug(
        String.format(
            "Querying local store for last commit in repository %1$s on branch %2$s",
            repositoryId, branchRef));

    Criteria criteria =
        getSession()
            .createCriteria(LastProcessedItem.class)
            // .add(Restrictions.eq("id", LAST_COMMIT_HASH + "||" + repositoryId))
            .add(Restrictions.eq("repositoryId", repositoryId))
            .add(Restrictions.eq("branchRef", branchRef));
    LastProcessedItem result = (LastProcessedItem) criteria.uniqueResult();
    getSession().evict(result);
    return result == null ? null : result.getValue();
  }
 @Override
 public <T extends GettableById> Number count(
     Class<T> currentClass, Map<String, List<Criterion>> restrictions) {
   validateTransaction();
   Criteria criteria = getSession().createCriteria(currentClass);
   criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
   criteria.setProjection(Projections.rowCount());
   for (String key : restrictions.keySet()) {
     if (key.equals(RestrictionsFields.MAIN_RESTRICTIONS)) {
       for (Criterion restriction : restrictions.get(RestrictionsFields.MAIN_RESTRICTIONS))
         criteria.add(restriction);
     } else if (restrictions.get(key).size() > 0) {
       Criteria c = criteria.createCriteria(key);
       for (Criterion restriction : restrictions.get(key)) c.add(restriction);
     }
   }
   return ((Number) criteria.uniqueResult());
 }
Ejemplo n.º 8
0
 @Override
 public <P> List<P> reportList(
     Class<P> projectClass, ProjectionList projectionList, Criteria criteria) {
   Criteria report = buildProjectionCriteria(projectClass, criteria, projectionList, false);
   return (List<P>) report.uniqueResult();
 }
Ejemplo n.º 9
0
 @Override
 public <P> P reportOne(Class<P> projectClass, ProjectionList projectionList, Criteria criteria) {
   Criteria report = buildProjectionCriteria(projectClass, criteria, projectionList, true);
   return (P) report.uniqueResult();
 }
Ejemplo n.º 10
0
 @Override
 public <T> T findUnique(Class<T> clazz, Criteria criteria) {
   return (T) criteria.uniqueResult();
 }
Ejemplo n.º 11
0
 @SuppressWarnings("unchecked")
 public T findFirst(Criteria criteria) {
   criteria.setFirstResult(0).setMaxResults(1);
   return (T) criteria.uniqueResult();
 }