本文整理汇总了Java中javax.persistence.criteria.CriteriaQuery类的典型用法代码示例。如果您正苦于以下问题:Java CriteriaQuery类的具体用法?Java CriteriaQuery怎么用?Java CriteriaQuery使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CriteriaQuery类属于javax.persistence.criteria包,在下文中一共展示了CriteriaQuery类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import javax.persistence.criteria.CriteriaQuery; //导入依赖的package包/类
public static void main(String[] args) {
EntityManagerFactory entityManagerFactory = Persistence
.createEntityManagerFactory("pl.edu.bogdan.training.db.entity");
EntityManager em = entityManagerFactory.createEntityManager();
em.getTransaction().begin();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<User> cq = cb.createQuery(User.class);
Root<User> from = cq.from(User.class);
Join<User, Role> join = from.join("role", JoinType.LEFT);
cq.where(cb.equal(join.get("name"), "ordinary"));
TypedQuery<User> tq = em.createQuery(cq);
List<User> users = tq.getResultList();
for (User u : users) {
System.out.println(u.getLastName());
}
em.getTransaction().commit();
em.close();
entityManagerFactory.close();
}
示例2: toPredicate
import javax.persistence.criteria.CriteriaQuery; //导入依赖的package包/类
@Override
public Predicate toPredicate(final Root<?> leveringsautorisatieRoot, final CriteriaQuery<?> query, final CriteriaBuilder cb) {
final Root<Dienstbundel> dienstbundelRoot = query.from(Dienstbundel.class);
final Root<Dienst> dienstRoot = query.from(Dienst.class);
// Join dienst op dienstbundel
final Predicate joinPredicateDienstbundel = dienstbundelRoot.get(ID).in(dienstRoot.get("dienstbundel").get(ID));
// Join dienst op leveringsautorisatie
final Predicate joinPredicateLeveringsautorisatie = leveringsautorisatieRoot.get(ID).in(dienstbundelRoot.get(LEVERINGSAUTORISATIE).get(ID));
// OntvangendePartij
final Predicate soortDienstPredicate = cb.equal(dienstRoot.get("soortDienstId"), value);
return cb.and(joinPredicateDienstbundel, joinPredicateLeveringsautorisatie, soortDienstPredicate);
}
示例3: findDeploymentSpecByVirtualSystemProjectAndRegion
import javax.persistence.criteria.CriteriaQuery; //导入依赖的package包/类
public static DeploymentSpec findDeploymentSpecByVirtualSystemProjectAndRegion(EntityManager em, VirtualSystem vs,
String projectId, String region) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<DeploymentSpec> query = cb.createQuery(DeploymentSpec.class);
Root<DeploymentSpec> root = query.from(DeploymentSpec.class);
query = query.select(root)
.where(cb.equal(root.get("projectId"), projectId),
cb.equal(root.get("region"), region),
cb.equal(root.get("virtualSystem"), vs));
try {
return em.createQuery(query).getSingleResult();
} catch (NoResultException nre) {
return null;
}
}
示例4: findByOpenstackId
import javax.persistence.criteria.CriteriaQuery; //导入依赖的package包/类
public static VM findByOpenstackId(EntityManager em, String id) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<VM> query = cb.createQuery(VM.class);
Root<VM> root = query.from(VM.class);
query = query.select(root)
.where(cb.equal(root.get("openstackId"), id));
try {
return em.createQuery(query).getSingleResult();
} catch (NoResultException nre) {
return null;
}
}
示例5: aggregate
import javax.persistence.criteria.CriteriaQuery; //导入依赖的package包/类
private <S> S aggregate(CriteriaBuilder builder, CriteriaQuery<S> query, Root<E> root, Specification<E> spec, List<Selection<?>> selectionList, LockModeType lockMode) {
if (selectionList != null) {
Predicate predicate = spec.toPredicate(root, query, builder);
if (predicate != null) {
query.where(predicate);
}
query.multiselect(selectionList);
return (S) em.createQuery(query).setLockMode(lockMode).getSingleResult();
}
return null;
}
示例6: retrieveAuthor
import javax.persistence.criteria.CriteriaQuery; //导入依赖的package包/类
/**
* gets an author from the database by determining the type of the provided id. if no author is present it builds one from the id.
* @param id the author identifier
* @return the author retrieved from the database or build with the identifier
* @throws JDOMException thrown upon parsing the source response
* @throws IOException thrown upon reading profiles from disc
* @throws SAXException thrown when parsing the files from disc
*/
public PublicationAuthor retrieveAuthor(String id) throws JDOMException, IOException, SAXException {
typeOfID = determineID(id);
LOGGER.info("given ID: " + id + " is of type " + typeOfID);
EntityManagerFactory emf = Persistence.createEntityManagerFactory("publicationAuthors");
EntityManager em = emf.createEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<PublicationAuthor> q = cb.createQuery(PublicationAuthor.class);
Root<PublicationAuthor> c = q.from(PublicationAuthor.class);
List<Predicate> predicates = new ArrayList<>();
if (typeOfID.equals("surname")) {
if (id.contains(",")) {
predicates.add(cb.equal(c.get("surname"),id.substring(0,id.indexOf(","))));
predicates.add(cb.equal(c.get("firstname"),id.substring(id.indexOf(",")+1)));
LOGGER.info("retriving surname, firstname from database for " + id);
} else if (id.contains(" ")) {
predicates.add(cb.equal(c.get("firstname"),id.substring(0,id.indexOf(" "))));
predicates.add(cb.equal(c.get("surname"),id.substring(id.indexOf(" ")+1)));
LOGGER.info("retrieving firstname surname from database for " + id);
} else {
predicates.add(cb.equal(c.get("surname"), id));
LOGGER.info("retrieving surname from database for " + id);
}
}
predicates.add(cb.equal(c.get(typeOfID), id));
q.select(c).where(cb.equal(c.get(typeOfID), id));
TypedQuery<PublicationAuthor> query = em.createQuery(q);
List<PublicationAuthor> authors = query.getResultList();
em.close();
if (authors.size() == 1) {
LOGGER.info("found author in database");
this.author = authors.get(0);
return author;
}
LOGGER.info("no match in database");
return buildAuthor(id);
}
示例7: findOneByProperty
import javax.persistence.criteria.CriteriaQuery; //导入依赖的package包/类
/**
* 根据某些属性获取对象L
* @param name 属性名称
* @param value 属性值
* @param lockMode 对象锁类型
* @return
*/
public T findOneByProperty(String name, Object value, LockModeType lockMode) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<T> query = cb.createQuery(entityClass);
Root<T> root = query.from(entityClass);
query.where(cb.equal(QueryFormHelper.getPath(root, name), value));
TypedQuery<T> typedQuery = em.createQuery(query);
typedQuery.setLockMode(lockMode);
try {
List<T> list = typedQuery.getResultList();
if (list.isEmpty()) {
return null;
} else {
return list.get(0);
}
} catch (NoResultException e) {
return null;
}
}
示例8: findApprovedCommunities
import javax.persistence.criteria.CriteriaQuery; //导入依赖的package包/类
@Override
public List<Community> findApprovedCommunities() {
LOG.info("findApprovedCommunites()");
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Community> criteria = builder.createQuery(Community.class);
Root<Community> community = criteria.from(Community.class);
try {
criteria.where(builder.equal(community.get("state"), new Enumeration(2)));
} catch (DatabaseException e1) {
LOG.error("Enumeration(2).APPROVED konnte nicht erstellt werden!");
}
TypedQuery<Community> query = em.createQuery(criteria);
try {
List <Community> coms = query.getResultList();
for(Community c : coms) {
initializeCom(c);
}
return coms;
} catch (NoResultException e) {
LOG.error(e.toString());
return null;
}
}
示例9: getPlayersByCity
import javax.persistence.criteria.CriteriaQuery; //导入依赖的package包/类
public List<PlayerDetails> getPlayersByCity(String city) {
logger.info("getPlayersByCity");
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);
// Get MetaModel from Root
//EntityType<Player> Player_ = player.getModel();
// set the where clause
cq.where(cb.equal(team.get(Team_.city), city));
cq.select(player).distinct(true);
TypedQuery<Player> q = em.createQuery(cq);
players = q.getResultList();
}
return copyPlayersToDetails(players);
} catch (Exception ex) {
throw new EJBException(ex);
}
}
示例10: getPolicy
import javax.persistence.criteria.CriteriaQuery; //导入依赖的package包/类
/**
* Gets the Policy for a given domain and the policy
*
* @return - Policy
* @throws Exception
*/
@Path("/{domainId}/policies/{policyId}")
@GET
public PolicyEntity getPolicy(@PathParam("domainId") Long domainId, @PathParam("policyId") Long policyId)
throws Exception {
LOG.info("getting Policy for Policy ID..:" + policyId);
return this.txControl.supports(new Callable<PolicyEntity>() {
@Override
public PolicyEntity call() throws Exception {
CriteriaBuilder criteriaBuilder = DomainApis.this.em.getCriteriaBuilder();
CriteriaQuery<PolicyEntity> query = criteriaBuilder.createQuery(PolicyEntity.class);
Root<PolicyEntity> r = query.from(PolicyEntity.class);
query.select(r).where(criteriaBuilder.and(criteriaBuilder.equal(r.get("domain").get("id"), domainId),
criteriaBuilder.equal(r.get("id"), policyId)));
List<PolicyEntity> result = DomainApis.this.em.createQuery(query).getResultList();
if (result.isEmpty()) {
throw new Exception("Policy or Domain Entity does not exists...");
//TODO - Add 404 error response - Sudhir
}
return result.get(0);
}
});
}
示例11: createQuery
import javax.persistence.criteria.CriteriaQuery; //导入依赖的package包/类
private TypedQuery<ConfigurationEntryDto> createQuery(String providerNid, String providerId, CloudTarget targetSpace, String mtaId,
EntityManager manager) {
CriteriaBuilder builder = manager.getCriteriaBuilder();
CriteriaQuery<ConfigurationEntryDto> query = builder.createQuery(ConfigurationEntryDto.class);
Root<ConfigurationEntryDto> root = query.from(ConfigurationEntryDto.class);
List<Predicate> predicates = new ArrayList<>();
if (providerNid != null) {
predicates.add(builder.equal(root.get(FieldNames.PROVIDER_NID), providerNid));
}
if (targetSpace != null && !StringUtils.isEmpty(targetSpace.getSpace())) {
predicates.add(builder.equal(root.get(FieldNames.TARGET_SPACE), targetSpace.getSpace()));
if (!StringUtils.isEmpty(targetSpace.getOrg())) {
predicates.add(builder.equal(root.get(FieldNames.TARGET_ORG), targetSpace.getOrg()));
}
}
if (providerId != null) {
predicates.add(builder.equal(root.get(FieldNames.PROVIDER_ID), providerId));
} else if (mtaId != null) {
predicates.add(builder.like(root.get(FieldNames.PROVIDER_ID), mtaId + ":%"));
}
return manager.createQuery(query.select(root).where(predicates.toArray(new Predicate[0])));
}
示例12: getPolicyList
import javax.persistence.criteria.CriteriaQuery; //导入依赖的package包/类
@Override
public List<PolicyEntity> getPolicyList(String domainId) throws Exception {
return this.txControl.supports(new Callable<List<PolicyEntity>>() {
@Override
public List<PolicyEntity> call() throws Exception {
DomainEntity result = IsmPolicyApi.this.em.find(DomainEntity.class, Long.parseLong(domainId));
if (result == null) {
throw new Exception("Domain Entity does not exists...");
//TODO - to add RETURN 404 error:Sudhir
}
CriteriaBuilder criteriaBuilder = IsmPolicyApi.this.em.getCriteriaBuilder();
CriteriaQuery<PolicyEntity> query = criteriaBuilder.createQuery(PolicyEntity.class);
Root<PolicyEntity> r = query.from(PolicyEntity.class);
query.select(r).where(criteriaBuilder
.and(criteriaBuilder.equal(r.get("domain").get("id"), Long.parseLong(domainId))));
return IsmPolicyApi.this.em.createQuery(query).getResultList();
}
});
}
示例13: readPage
import javax.persistence.criteria.CriteriaQuery; //导入依赖的package包/类
/**
* Returns a page of objects.
*
* @param entityClass The entity class.
* @param page The page index (zero indexed).
* @param pageSize The page size.
* @return A page of objects.
*/
@Override
public <T extends BaseEntity> List<T> readPage(
final Class<T> entityClass,
final int page,
final int pageSize) {
final CriteriaBuilder cb = em.getCriteriaBuilder();
final CriteriaQuery<T> cq = cb.createQuery(entityClass);
final Root<T> root = cq.from(entityClass);
cq.select(root);
cq.orderBy(cb.desc(root.get("id")));
return em.createQuery(cq)
.setFirstResult(page * pageSize)
.setMaxResults(pageSize)
.getResultList();
}
示例14: findByApplianceVersionVirtTypeAndVersion
import javax.persistence.criteria.CriteriaQuery; //导入依赖的package包/类
public static ApplianceSoftwareVersion findByApplianceVersionVirtTypeAndVersion(EntityManager em, Long applianceId, String av,
VirtualizationType vt, String vv) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<ApplianceSoftwareVersion> query = cb.createQuery(ApplianceSoftwareVersion.class);
Root<ApplianceSoftwareVersion> root = query.from(ApplianceSoftwareVersion.class);
query = query.select(root)
.where(cb.equal(root.join("appliance").get("id"), applianceId),
cb.equal(cb.upper(root.get("applianceSoftwareVersion")), av.toUpperCase()),
cb.equal(root.get("virtualizationType"), vt),
cb.equal(cb.upper(root.get("virtualizationSoftwareVersion")), vv.toUpperCase())
);
try {
return em.createQuery(query).getSingleResult();
} catch (NoResultException nre) {
return null;
}
}
示例15: toPredicate
import javax.persistence.criteria.CriteriaQuery; //导入依赖的package包/类
@Override
public Predicate toPredicate(final Root<?> persoonRoot, final CriteriaQuery<?> query, final CriteriaBuilder cb) {
final Root<PersoonAdres> persoonAdressenRoot = query.from(PersoonAdres.class);
final Predicate joinPredicateAdressen = persoonRoot.get(ID).in(persoonAdressenRoot.get(PERSOON).get(ID));
final Predicate adresPredicate;
if (likePredicate) {
final Expression<String> attributeExpression = cb.lower(cb.function(UNACCENT, String.class, persoonAdressenRoot.get(field)));
final Expression<String> valueExpression =
cb.lower(
cb.function(
UNACCENT,
String.class,
cb.concat(cb.concat(cb.literal(WILDCARD), cb.literal(value.toString())), cb.literal(WILDCARD))));
adresPredicate = cb.like(attributeExpression, valueExpression);
} else {
adresPredicate = cb.equal(persoonAdressenRoot.get(field), value);
}
return cb.and(joinPredicateAdressen, adresPredicate);
}