本文整理汇总了Java中javax.persistence.criteria.CriteriaQuery.where方法的典型用法代码示例。如果您正苦于以下问题:Java CriteriaQuery.where方法的具体用法?Java CriteriaQuery.where怎么用?Java CriteriaQuery.where使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.persistence.criteria.CriteriaQuery
的用法示例。
在下文中一共展示了CriteriaQuery.where方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: _criteria
import javax.persistence.criteria.CriteriaQuery; //导入方法依赖的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);
}
示例2: getPlayersBySport
import javax.persistence.criteria.CriteriaQuery; //导入方法依赖的package包/类
public List<PlayerDetails> getPlayersBySport(String sport) {
logger.info("getPlayersByLeagueId");
List<Player> players = null;
try {
CriteriaQuery<Player> cq = cb.createQuery(Player.class);
if (cq != null) {
Root<Player> player = cq.from(Player.class);
Join<Player, Team> team = player.join(Player_.team);
Join<Team, League> league = team.join(Team_.league);
// Get MetaModel from Root
//EntityType<Player> Player_ = player.getModel();
// set the where clause
cq.where(cb.equal(league.get(League_.sport), sport));
cq.select(player).distinct(true);
TypedQuery<Player> q = em.createQuery(cq);
players = q.getResultList();
}
return copyPlayersToDetails(players);
} catch (Exception ex) {
throw new EJBException(ex);
}
}
示例3: zoekLo3VoorkomenVoorActie
import javax.persistence.criteria.CriteriaQuery; //导入方法依赖的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);
}
示例4: applySpecificationToQueryCriteria
import javax.persistence.criteria.CriteriaQuery; //导入方法依赖的package包/类
/**
* Spec op query doen.
* @param spec spec
* @param query query
* @param <S> query type
* @return root
*/
protected final <S> Root<T> applySpecificationToQueryCriteria(final Specification<T> spec, final CriteriaQuery<S> query) {
Assert.notNull(query, "Query mag niet null zijn.");
final Root<T> root = query.from(getDomainClass());
if (spec == null) {
return root;
}
final CriteriaBuilder builder = em.getCriteriaBuilder();
final Predicate predicate = spec.toPredicate(root, query, builder);
if (predicate != null) {
query.where(predicate);
}
return root;
}
示例5: AuthorizationService
import javax.persistence.criteria.CriteriaQuery; //导入方法依赖的package包/类
public AuthorizationService(@Context HttpHeaders headers) {
authorization = false;
String token = headers.getHeaderString("Authorization");
if (token != null) {
try {
Session session = HibernateUtil.getSessionFactory().openSession();
CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<User> criteria = builder.createQuery(User.class);
Root<User> root = criteria.from(User.class);
criteria.select(root);
criteria.where(builder.equal(root.get(User_.token), token));
authenticate = session.createQuery(criteria).getSingleResult();
if (authenticate != null)
authorization = true;
} catch (Exception e) {
}
}
}
示例6: search
import javax.persistence.criteria.CriteriaQuery; //导入方法依赖的package包/类
public List<ClanEntity> search(String name, int minMembers, int maxMembers, int minTrophies, boolean onlyJoinable) {
try (Session session = session()) {
CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<ClanEntity> query = builder.createQuery(ClanEntity.class);
Root<ClanEntity> root = query.from(ClanEntity.class);
query.select(root);
if (name != null && !name.isEmpty()) {
query.where(builder.like(root.get("name"), "%" + name + "%"));
}
// TODO: Check min and max members
if (minTrophies != 0) {
query.where(builder.greaterThan(root.get("score"), minTrophies));
}
if (onlyJoinable) {
query.where(builder.equal(root.get("type"), builder.literal(ClanType.OPEN)));
// TODO: Check members
}
return session.createQuery(query).getResultList();
}
}
示例7: getPlayersByLeagueId
import javax.persistence.criteria.CriteriaQuery; //导入方法依赖的package包/类
public List<PlayerDetails> getPlayersByLeagueId(String leagueId) {
logger.info("getPlayersByLeagueId");
List<Player> players = null;
try {
CriteriaQuery<Player> cq = cb.createQuery(Player.class);
if (cq != null) {
Root<Player> player = cq.from(Player.class);
Join<Player, Team> team = player.join(Player_.teams);
Join<Team, League> league = team.join(Team_.league);
// Get MetaModel from Root
//EntityType<Player> Player_ = player.getModel();
// set the where clause
cq.where(cb.equal(league.get(League_.id), leagueId));
cq.select(player).distinct(true);
TypedQuery<Player> q = em.createQuery(cq);
players = q.getResultList();
}
return copyPlayersToDetails(players);
} catch (Exception ex) {
throw new EJBException(ex);
}
}
示例8: getStatusEntriesByDate
import javax.persistence.criteria.CriteriaQuery; //导入方法依赖的package包/类
public List<StatusEntry> getStatusEntriesByDate(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
CriteriaQuery<StatusEntry> cq = cb.createQuery(StatusEntry.class);
Root<StatusEntry> statusEntry = cq.from(StatusEntry.class);
cq.select(statusEntry);
cq.where(cb.equal(statusEntry.get(StatusEntry_.statusDate), cal));
cq.distinct(true);
TypedQuery<StatusEntry> q = em.createQuery(cq);
return q.getResultList();
}
示例9: createQueryWrapper
import javax.persistence.criteria.CriteriaQuery; //导入方法依赖的package包/类
public static <T> QueryWrapper createQueryWrapper(List<AbstractExpression> expressions) {
return (Root<?> root, CriteriaQuery<?> query, CriteriaBuilder cb, boolean sort)-> {
List<Predicate> predicates = new ArrayList<>();
expressions.forEach(expression->
predicates.add(expression.buildJpaPredicate(cb,root))
);
Predicate[] array = new Predicate[predicates.size()];
predicates.toArray(array);
query.where(array);
};
}
示例10: getPlayersByHigherSalary
import javax.persistence.criteria.CriteriaQuery; //导入方法依赖的package包/类
public List<PlayerDetails> getPlayersByHigherSalary(String name) {
logger.info("getPlayersByHigherSalary");
List<Player> players = null;
try {
CriteriaQuery<Player> cq = cb.createQuery(Player.class);
if (cq != null) {
Root<Player> player1 = cq.from(Player.class);
Root<Player> player2 = cq.from(Player.class);
// Get MetaModel from Root
//EntityType<Player> Player_ = player1.getModel();
// create a Predicate object that finds players with a salary
// greater than player1
Predicate gtPredicate = cb.greaterThan(
player1.get(Player_.salary),
player2.get(Player_.salary));
// create a Predicate object that finds the player based on
// the name parameter
Predicate equalPredicate = cb.equal(
player2.get(Player_.name),
name);
// set the where clause with the predicates
cq.where(gtPredicate, equalPredicate);
// set the select clause, and return only unique entries
cq.select(player1).distinct(true);
TypedQuery<Player> q = em.createQuery(cq);
players = q.getResultList();
}
return copyPlayersToDetails(players);
} catch (Exception ex) {
throw new EJBException(ex);
}
}
示例11: txPortEntityByElementId
import javax.persistence.criteria.CriteriaQuery; //导入方法依赖的package包/类
public PortEntity txPortEntityByElementId(String elementId) {
CriteriaBuilder cb = this.em.getCriteriaBuilder();
CriteriaQuery<PortEntity> q = cb.createQuery(PortEntity.class);
Root<PortEntity> r = q.from(PortEntity.class);
q.where(cb.equal(r.get("elementId"), elementId));
try {
return this.em.createQuery(q).getSingleResult();
} catch (Exception e) {
LOG.error("Finding Network Element {} ", elementId, e);
return null;
}
}
示例12: findAllByFieldIn
import javax.persistence.criteria.CriteriaQuery; //导入方法依赖的package包/类
@Override
public List<T> findAllByFieldIn(String Field, Iterable<?> values) {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<T> query = cb.createQuery(getDomainClass());
Root<T> e = query.from(getDomainClass());
query.where(cb.in(e.get(Field)).value(values));
return entityManager.createQuery(query).getResultList();
}
示例13: applySpecificationToCriteria
import javax.persistence.criteria.CriteriaQuery; //导入方法依赖的package包/类
/**
* Applies the given {@link Specification} to the given {@link CriteriaQuery}.
*/
private <S, T> Root<T> applySpecificationToCriteria(final Root<T> root, final Specification<T> spec, final CriteriaQuery<S> query) {
Assert.notNull(query, "Query is requested");
if (spec != null) {
// There is at least one described filter
final CriteriaBuilder builder = em.getCriteriaBuilder();
final Predicate predicate = spec.toPredicate(root, query, builder);
// There is at least one validated filter
query.where(predicate);
}
return root;
}
示例14: getTodaysSession
import javax.persistence.criteria.CriteriaQuery; //导入方法依赖的package包/类
public TutoringSession getTodaysSession() {
// get today's date and trim the time information
Calendar today = Calendar.getInstance();
CalendarUtil.stripTime(today);
// find today's session
SimpleDateFormat formatter = new SimpleDateFormat("E, MMM d, yyyy");
logger.log(Level.INFO, "Finding tutoring session for {0}", formatter.format(today.getTime()));
CriteriaQuery<TutoringSession> cq = cb.createQuery(TutoringSession.class);
Root<TutoringSession> tutoringSession = cq.from(TutoringSession.class);
cq.select(tutoringSession);
cq.where(cb.equal(tutoringSession.get(TutoringSession_.sessionDate), today));
cq.distinct(true);
TypedQuery<TutoringSession> q = em.createQuery(cq);
TutoringSession session;
try {
session = q.getSingleResult();
logger.info("Found session for today.");
} catch (NoResultException e) {
logger.info("Today's session not found. Creating a new session.");
session = new TutoringSession();
em.persist(session);
}
return session;
}
示例15: readByName
import javax.persistence.criteria.CriteriaQuery; //导入方法依赖的package包/类
public UserDataSet readByName(String name) {
CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<UserDataSet> criteria = builder.createQuery(UserDataSet.class);
Root<UserDataSet> from = criteria.from(UserDataSet.class);
criteria.where(builder.equal(from.get("name"), name));
Query<UserDataSet> query = session.createQuery(criteria);
return query.uniqueResult();
}