本文整理匯總了Java中javax.persistence.criteria.CriteriaBuilder類的典型用法代碼示例。如果您正苦於以下問題:Java CriteriaBuilder類的具體用法?Java CriteriaBuilder怎麽用?Java CriteriaBuilder使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
CriteriaBuilder類屬於javax.persistence.criteria包,在下文中一共展示了CriteriaBuilder類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: findRecentRegionFrom
import javax.persistence.criteria.CriteriaBuilder; //導入依賴的package包/類
@GET
@Produces({"application/xml", "application/json"})
@Path("/recent/region/{regionName}/{orderLineId}")
public List<LiveSalesList> findRecentRegionFrom(@PathParam("regionName") String regionName, @PathParam("orderLineId") Integer orderLineId) {
CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
javax.persistence.criteria.CriteriaQuery cq = cb.createQuery();
Root<LiveSalesList> liveSalesList = cq.from(LiveSalesList.class);
cq.select(liveSalesList);
cq.where(cb.and(
cb.equal(liveSalesList.get(LiveSalesList_.region), regionName),
cb.gt(liveSalesList.get(LiveSalesList_.orderLineId), orderLineId)
));
Query q = getEntityManager().createQuery(cq);
q.setMaxResults(500);
return q.getResultList();
}
示例2: toPredicate
import javax.persistence.criteria.CriteriaBuilder; //導入依賴的package包/類
@Override
public Predicate toPredicate(Root<TestSuite> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
Predicate result = null;
if (StringUtils.isNotEmpty(filter.getSuite())) {
result = criteriaBuilder.like(criteriaBuilder.lower(root.get(SUITE_PROPERTY)),
buildLikeValue(filter.getSuite()));
}
return result;
}
示例3: findByWordAndPackageNo
import javax.persistence.criteria.CriteriaBuilder; //導入依賴的package包/類
public Collection<ExtGraph> findByWordAndPackageNo(String word, int packageno) {
Long pkg = new Long(packageno);
CriteriaBuilder criteriaBuilder = getEntityManager().getCriteriaBuilder();
CriteriaQuery<ExtGraph> criteriaQuery = criteriaBuilder.createQuery(ExtGraph.class
);
Root<ExtGraph> root = criteriaQuery.from(ExtGraph.class);
Join<ExtGraph, Synset> synset = root.join("synset", JoinType.INNER);
Join<Synset, Sense> sts = synset.join("sense", JoinType.LEFT);
List<Predicate> criteriaList = new ArrayList<>();
Predicate firstCondition = criteriaBuilder.equal(root.get("word"), word);
criteriaList.add(firstCondition);
Predicate secondCondition = criteriaBuilder.equal(root.get("packageno"), pkg);
criteriaList.add(secondCondition);
Predicate thirdCondition = criteriaBuilder.equal(sts.get("senseIndex"), 0);
criteriaList.add(thirdCondition);
criteriaQuery.where(criteriaBuilder.and(criteriaList.toArray(new Predicate[0])));
final TypedQuery<ExtGraph> query = getEntityManager().createQuery(criteriaQuery);
return query.getResultList();
}
示例4: filterByKeywordAndStatus
import javax.persistence.criteria.CriteriaBuilder; //導入依賴的package包/類
public static Specification<Post> filterByKeywordAndStatus(
final String keyword,//
final Post.Status status) {
return (Root<Post> root, CriteriaQuery<?> query, CriteriaBuilder cb) -> {
List<Predicate> predicates = new ArrayList<>();
if (StringUtils.hasText(keyword)) {
predicates.add(
cb.or(
cb.like(root.get(Post_.title), "%" + keyword + "%"),
cb.like(root.get(Post_.content), "%" + keyword + "%")
)
);
}
if (status != null) {
predicates.add(cb.equal(root.get(Post_.status), status));
}
return cb.and(predicates.toArray(new Predicate[predicates.size()]));
};
}
示例5: toPredicate
import javax.persistence.criteria.CriteriaBuilder; //導入依賴的package包/類
@SuppressWarnings({ "rawtypes", "unchecked" })
public Predicate toPredicate(Root<?> root, CriteriaQuery<?> query,
CriteriaBuilder builder) {
Path expression = null;
if(fieldName.contains(".")){
String[] names = StringUtils.split(fieldName, ".");
expression = root.get(names[0]);
for (int i = 1; i < names.length; i++) {
expression = expression.get(names[i]);
}
}else{
expression = root.get(fieldName);
}
switch (operator) {
case EQ:
return builder.equal(expression, value);
case NE:
return builder.notEqual(expression, value);
case LIKE:
return builder.like((Expression<String>) expression, "%" + value + "%");
case LT:
return builder.lessThan(expression, (Comparable) value);
case GT:
return builder.greaterThan(expression, (Comparable) value);
case LTE:
return builder.lessThanOrEqualTo(expression, (Comparable) value);
case GTE:
return builder.greaterThanOrEqualTo(expression, (Comparable) value);
default:
return null;
}
}
示例6: search
import javax.persistence.criteria.CriteriaBuilder; //導入依賴的package包/類
@Override
public Page search(QueryCriteria criteria, Pageable pageable) {
CriteriaBuilder builder = this.entityManager.getCriteriaBuilder();
CriteriaQuery<Long> countCriteria = builder.createQuery(Long.class);
Root<T> countRoot = countCriteria.from(this.domainClass);
long total = this.entityManager.createQuery(
countCriteria.select(builder.count(countRoot))
.where(toPredicates(criteria, countRoot, builder))
).getSingleResult();
CriteriaQuery<T> pageCriteria = builder.createQuery(this.domainClass);
Root<T> pageRoot = pageCriteria.from(this.domainClass);
List<T> list = this.entityManager.createQuery(
pageCriteria.select(pageRoot)
.where(toPredicates(criteria, pageRoot, builder))
.orderBy(toOrders(pageable.getSort(), pageRoot, builder))
).setFirstResult(pageable.getOffset())
.setMaxResults(pageable.getPageSize())
.getResultList();
return new PageImpl<>(new ArrayList<>(list), pageable, total);
}
示例7: _criteria
import javax.persistence.criteria.CriteriaBuilder; //導入依賴的package包/類
private static void _criteria(DbService dbService) {
final EntityManager em = dbService.getEntityManager();
// INSERT new record
em.getTransaction().begin();
em.persist(new Account("A"));
em.persist(new Account("B"));
em.persist(new Account("C"));
em.getTransaction().commit();
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Account> query = builder.createQuery(Account.class);
Root<Account> acc = query.from(Account.class);
Predicate cond = builder.gt(acc.get("id"), 1);
query.where(cond);
TypedQuery<Account> q = em.createQuery(query);
List<Account> resultList = q.getResultList();
System.out.println(resultList);
}
示例8: validarUsuario
import javax.persistence.criteria.CriteriaBuilder; //導入依賴的package包/類
public Pessoa validarUsuario(String log, String pass){
final CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
final CriteriaQuery<Pessoa> cquery = cb.createQuery(Pessoa.class);
final Root<Pessoa> root = cquery.from(Pessoa.class);
final List<Predicate> condicoes = new ArrayList<Predicate>();
condicoes.add(cb.equal(root.get("usuario").get("login"), log));
condicoes.add(cb.equal(root.get("usuario").get("senha"), pass));
cquery.select(root).where(condicoes.toArray(new Predicate[]{}));
Pessoa pessoa = new Pessoa();
try{
pessoa = getEntityManager().createQuery(cquery).getSingleResult();
} catch (Exception e) {
throw new QueryTimeoutException("Usuário ou senha invalido!");
}
return pessoa;
}
示例9: applyOrder
import javax.persistence.criteria.CriteriaBuilder; //導入依賴的package包/類
/**
* Apply ordering criteria.
*/
private <T> void applyOrder(final UiPageRequest uiPageRequest, final Map<String, String> mapping, final CriteriaBuilder builder,
final CriteriaQuery<T> query, final Root<T> root) {
// Apply the sort
if (uiPageRequest.getUiSort() != null) {
// Need to order the result
final UiSort uiSort = uiPageRequest.getUiSort();
final String ormColumn = mapping.get(uiSort.getColumn());
if (ormColumn != null) {
// ORM column is validated
final Sort sort = new Sort(uiSort.getDirection(), mapping.get(uiSort.getColumn()));
query.orderBy(QueryUtils.toOrders(sort, root, builder));
}
}
}
示例10: zoekLo3VoorkomenVoorActie
import javax.persistence.criteria.CriteriaBuilder; //導入依賴的package包/類
@Override
public final Lo3Voorkomen zoekLo3VoorkomenVoorActie(final Long actieId) {
final CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
final CriteriaQuery<Lo3Voorkomen> criteria = criteriaBuilder.createQuery(Lo3Voorkomen.class);
final Root<Lo3Voorkomen> criteriaRoot = criteria.from(Lo3Voorkomen.class);
criteria.select(criteriaRoot);
criteria.where(new Predicate[] {criteriaBuilder.equal(criteriaRoot.get("actie").get("id"), actieId) });
final List<Lo3Voorkomen> result = em.createQuery(criteria).getResultList();
return result.isEmpty() ? null : result.get(0);
}
示例11: getPermission
import javax.persistence.criteria.CriteriaBuilder; //導入依賴的package包/類
/**
* retrieves the <code>RolesPermission</code> by the role.
*
* @param role
* the role
* @return userPermissions the list of permissions of the user
*
*/
public static Set<String> getPermission(String role) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("userData");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<RolePermission> q = cb.createQuery(RolePermission.class);
Root<RolePermission> c = q.from(RolePermission.class);
q.select(c).where(cb.equal(c.get("roleName"), role));
TypedQuery<RolePermission> query = em.createQuery(q);
List<RolePermission> permissions = query.getResultList();
Set<String> userPermissions = new HashSet<String>();
for (RolePermission permission : permissions)
userPermissions.add(permission.getPermission());
tx.commit();
em.close();
return userPermissions;
}
示例12: findExternalId
import javax.persistence.criteria.CriteriaBuilder; //導入依賴的package包/類
public static Pod findExternalId(EntityManager em, String externalId) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Pod> query = cb.createQuery(Pod.class);
Root<Pod> root = query.from(Pod.class);
query = query.select(root)
.where(cb.equal(root.get("externalId"), externalId));
try {
return em.createQuery(query).getSingleResult();
} catch (NoResultException nre) {
return null;
}
}
示例13: countDefault
import javax.persistence.criteria.CriteriaBuilder; //導入依賴的package包/類
private long countDefault(final Specification<T> specification, final DatatableQuery<?, T> input) {
final EntityManager entityManager = entityManagerFactory.createEntityManager();
final CriteriaBuilder qb = entityManager.getCriteriaBuilder();
final CriteriaQuery<Long> cq = qb.createQuery(Long.class);
final Root<T> from = cq.from(this.entity);
cq.select(qb.count(from));
if (specification != null) {
cq.where(specification.toPredicate(from, cq, qb));
}
final long ret = entityManager.createQuery(cq).getSingleResult();
entityManager.close();
return ret;
}
示例14: findRecentProductTypeFrom
import javax.persistence.criteria.CriteriaBuilder; //導入依賴的package包/類
@GET
@Produces({"application/xml", "application/json"})
@Path("/recent/producttype/{id}/{orderLineId}")
public List<LiveSalesList> findRecentProductTypeFrom(@PathParam("id") Integer productTypeId, @PathParam("orderLineId") Integer orderLineId) {
CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
javax.persistence.criteria.CriteriaQuery cq = cb.createQuery();
Root<LiveSalesList> liveSalesList = cq.from(LiveSalesList.class);
cq.select(liveSalesList);
cq.where(cb.and(
cb.equal(liveSalesList.get(LiveSalesList_.productTypeId), productTypeId),
cb.gt(liveSalesList.get(LiveSalesList_.orderLineId), orderLineId)
));
Query q = getEntityManager().createQuery(cq);
q.setMaxResults(500);
return q.getResultList();
}
示例15: findRecentRegionProductType
import javax.persistence.criteria.CriteriaBuilder; //導入依賴的package包/類
@GET
@Produces({"application/xml", "application/json"})
@Path("/recent/region/producttype/{regionName}/{productTypeId}")
public List<LiveSalesList> findRecentRegionProductType(@PathParam("regionName") String regionName, @PathParam("productTypeId") Integer productTypeId) {
CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
javax.persistence.criteria.CriteriaQuery cq = cb.createQuery();
Root<LiveSalesList> liveSalesList = cq.from(LiveSalesList.class);
cq.select(liveSalesList);
cq.where(cb.and(
cb.equal(liveSalesList.get(LiveSalesList_.productTypeId), productTypeId),
cb.equal(liveSalesList.get(LiveSalesList_.region), regionName)
));
Query q = getEntityManager().createQuery(cq);
q.setMaxResults(500);
return q.getResultList();
}