1. getCriteriaBuilder
CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
2. createQuery
CriteriaQuery<TrainOrder> query = builder.createQuery(TrainOrder.class);
3. Compose SQL
1) table
Root table = query.from(TrainOrder.class);
2) Condition
List<Predicate> predicate = new ArrayList<Predicate>(); predicate.add(builder.gt(table.get("trainNumber"), trainNumber)); predicate.add(builder.gt(table.get("seriaryNumber"), seriaryNumber)); Predicate[] condition = new Predicate[predicate.size()];
3) SQL
query.where(builder.and(predicate.toArray(condition)));
4. result
TypedQuery<TrainOrder> result = getEntityManager().createQuery(query); return (TrainOrder) result.getResultList();
原文:http://www.cnblogs.com/xiaobin-hlj80/p/6696978.html