List of usage examples for javax.persistence.criteria CriteriaBuilder createQuery
<T> CriteriaQuery<T> createQuery(Class<T> resultClass);
CriteriaQuery
object with the specified result type. From source file:br.ufba.dcc.mestrado.computacao.repository.impl.LicenseRepositoryImpl.java
@Override public OpenHubLicenseEntity findByName(String name) { CriteriaBuilder criteriaBuilder = getEntityManager().getCriteriaBuilder(); CriteriaQuery<OpenHubLicenseEntity> criteriaQuery = criteriaBuilder.createQuery(getEntityClass()); Root<OpenHubLicenseEntity> root = criteriaQuery.from(getEntityClass()); CriteriaQuery<OpenHubLicenseEntity> select = criteriaQuery.select(root); Predicate namePredicate = criteriaBuilder.equal(root.get("name"), name); select.where(namePredicate);/*from w ww .j a v a2 s . c o m*/ TypedQuery<OpenHubLicenseEntity> query = getEntityManager().createQuery(criteriaQuery); OpenHubLicenseEntity result = null; try { result = query.getSingleResult(); } catch (NoResultException ex) { } catch (NonUniqueResultException ex) { } return result; }
From source file:de.logicline.splash.dao.UserDaoImpl.java
public UserEntity getUser(String token) { CriteriaBuilder cb = getEntityManager().getCriteriaBuilder(); CriteriaQuery<UserEntity> cq = cb.createQuery(UserEntity.class); Root<UserEntity> user = cq.from(UserEntity.class); cq.select(user);//from w w w. j a v a 2s . co m cq.where(cb.equal(user.get("token"), token)); List<UserEntity> resultList = getEntityManager().createQuery(cq).getResultList(); UserEntity result = null; if (resultList != null && resultList.size() > 0) { result = resultList.get(0); return result; } return null; }
From source file:de.logicline.splash.dao.UserDaoImpl.java
public UserEntity getUserByName(String username) { CriteriaBuilder cb = getEntityManager().getCriteriaBuilder(); CriteriaQuery<UserEntity> cq = cb.createQuery(UserEntity.class); Root<UserEntity> user = cq.from(UserEntity.class); cq.select(user);//from ww w . ja v a 2s .c o m cq.where(cb.equal(user.get("username"), username)); List<UserEntity> resultList = getEntityManager().createQuery(cq).getResultList(); if (resultList == null || resultList.size() == 0) { log.debug("UserEntity not found"); return null; } else if (resultList.size() > 1) { log.warn("Multiple Users found for User with Username: " + username); return null; } return resultList.get(0); }
From source file:eu.uqasar.service.company.CompanyService.java
/** * // ww w .j av a 2s . c o m * @param innovationObjective * @return */ public List<Company> getAllByAscendingNameFiltered(CompanyFilterStructure filter, int first, int count) { logger.infof("loading all Companies ordered by ascending name matching the given filter %s...", filter); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Company> criteria = cb.createQuery(Company.class); Root<Company> root = criteria.from(Company.class); criteria.where(cb.or(cb.equal(root.get(Company_.name), filter.getName()), cb.equal(root.get(Company_.shortName), filter.getShortName()), cb.equal(root.get(Company_.country), filter.getCountry())) ); return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList(); }
From source file:com.sixsq.slipstream.persistence.Run.java
public static int viewListCount(User user, String moduleResourceUri, String cloudServiceName) throws ConfigurationException, ValidationException { int count = 0; EntityManager em = PersistenceUtil.createEntityManager(); try {/*from www. j a v a 2 s .c om*/ CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<Long> critQuery = builder.createQuery(Long.class); Root<Run> rootQuery = critQuery.from(Run.class); critQuery.select(builder.count(rootQuery)); Predicate where = viewListCommonQueryOptions(builder, rootQuery, user, moduleResourceUri, cloudServiceName); if (where != null) { critQuery.where(where); } TypedQuery<Long> query = em.createQuery(critQuery); count = (int) (long) query.getSingleResult(); } finally { em.close(); } return count; }
From source file:de.ks.idnadrev.information.view.InformationOverviewDS.java
protected List<Tag> getTags(List<String> tagNames, EntityManager em) { CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<Tag> query = builder.createQuery(Tag.class); Root<Tag> root = query.from(Tag.class); Path<String> namePath = root.get(KEY_NAME); query.select(root);// w w w .ja va 2 s. c om query.where(namePath.in(tagNames)); return em.createQuery(query).getResultList(); }
From source file:com.samples.platform.core.SystemUserInitDao.java
/** * Get the {@link AuthenticationType}s out of the database. * * @param enabled//from w w w . j a va 2s.co m * if not <code>null</code> and <code>true</code> only the * enabled {@link AuthenticationType}s are replied. * @return the list of {@link AuthenticationType}s. */ @Transactional(value = EipPersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED) public void enterSystemUser(final String contextName, final String userName, final String password, final String... roleNames) { AuthenticationType ac = this.of.createAuthenticationType(); ac.setContext(contextName); ac.setEnabled(true); GrantedAuthorityType r; for (String roleName : roleNames) { r = this.of.createGrantedAuthorityType(); r.setRoleName(roleName); ac.getGrantedAuthority().add(r); } ac.setPassword(password); ac.setUserName(userName); CriteriaBuilder cb = this.em.getCriteriaBuilder(); CriteriaQuery<AuthenticationType> q = cb.createQuery(AuthenticationType.class); Root<AuthenticationType> c = q.from(AuthenticationType.class); Predicate ands = cb.conjunction(); ands.getExpressions().add(cb.equal(c.<String>get(AuthenticationType_.context), contextName)); ands.getExpressions().add(cb.equal(c.<String>get(AuthenticationType_.userName), userName)); q.where(ands); q.orderBy(cb.asc(c.<String>get(AuthenticationType_.userName))); TypedQuery<AuthenticationType> typedQuery = this.em.createQuery(q); try { AuthenticationType stored = typedQuery.getSingleResult(); if (stored != null) { this.em.persist(ac); } } catch (NoResultException e) { this.em.persist(ac); } }
From source file:fi.vm.sade.organisaatio.dao.impl.YhteystietoElementtiDAOImpl.java
@Override public List<YhteystietoElementti> findAllKaytossa() { // Query query = getEntityManager().createQuery("SELECT x FROM YhteystietoElementti x where x.kaytossa = true"); // return query.getResultList(); CriteriaBuilder cb = getEntityManager().getCriteriaBuilder(); CriteriaQuery<YhteystietoElementti> query = cb.createQuery(YhteystietoElementti.class); Root<YhteystietoElementti> root = query.from(YhteystietoElementti.class); query.select(root);//from w ww. ja va2 s .c o m Predicate whereClause = cb.equal(root.get("kaytossa"), true); query.where(whereClause); return getEntityManager().createQuery(query).getResultList(); }
From source file:fi.vm.sade.organisaatio.dao.impl.YhteystietoArvoDAOImpl.java
/** * Returns yhteystietoarvos for a given yhteystietojen tyyppi * @param yhteystietojenTyyppi the yhteystietojen tyyppi given * @return the yhteystietoarvo objects matching the given yhteystietojen tyyppi *//* www.j ava 2 s .c o m*/ @Override public List<YhteystietoArvo> findByYhteystietojenTyyppi(YhteystietojenTyyppi yhteystietojenTyyppi) { CriteriaBuilder cb = getEntityManager().getCriteriaBuilder(); CriteriaQuery<YhteystietoArvo> query = cb.createQuery(YhteystietoArvo.class); Root<YhteystietoArvo> root = query.from(YhteystietoArvo.class); query.select(root); Predicate yhteystietojenTyyppiEquals = cb.equal(root.get("kentta").get("yhteystietojenTyyppi").get("oid"), yhteystietojenTyyppi.getOid()); query.where(yhteystietojenTyyppiEquals); return getEntityManager().createQuery(query).getResultList(); }
From source file:org.wallride.repository.CategoryRepositoryImpl.java
@Override public void lock(long id) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Long> query = cb.createQuery(Long.class); Root<Category> root = query.from(Category.class); query.select(root.get(Category_.id)); query.where(cb.equal(root.get(Category_.id), id)); entityManager.createQuery(query).setLockMode(LockModeType.PESSIMISTIC_WRITE).getSingleResult(); }