本文整理汇总了Java中javax.persistence.criteria.Selection类的典型用法代码示例。如果您正苦于以下问题:Java Selection类的具体用法?Java Selection怎么用?Java Selection使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Selection类属于javax.persistence.criteria包,在下文中一共展示了Selection类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: findByCriteriaAndMapConstructor
import javax.persistence.criteria.Selection; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public <T extends BaseEntity, C> List<C> findByCriteriaAndMapConstructor(ConstructorCriteria<T, C> criteria) {
EntityManager em = this.emf.createEntityManager();
try {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<C> cq = cb.createQuery(criteria.getConstructorClass());
Root<T> root = cq.from(criteria.getEntity());
return em.createQuery(cq.select(cb.construct(criteria.getConstructorClass(), criteria.getSelections()
.stream()
.map(root::get)
.collect(Collectors.toList())
.toArray(new Selection[LEN_ZERO])))
.where(JpaUtil.getPredicates(criteria.getCriteriaAttributes(), cb, root)))
.getResultList();
} catch (RuntimeException ex) {
LOGGER.error(ex.getMessage(), ex);
throw new PersistenceException(ex.getMessage(), ex);
} finally {
JpaUtil.closeEntityManager(em);
}
}
示例2: aggregate
import javax.persistence.criteria.Selection; //导入依赖的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;
}
示例3: select
import javax.persistence.criteria.Selection; //导入依赖的package包/类
@Override
public T select(Object... selections) {
if (!beforeMethodInvoke()) {
return (T) this;
}
List<Selection<?>> list = new ArrayList<Selection<?>>(selections.length);
for (Object selection : selections) {
if (selection instanceof String) {
parseSelectionStr(list, (String) selection);
} else if (selection instanceof Selection) {
list.add((Selection<?>) selection);
}
}
select(list.toArray(new Selection<?>[list.size()]));
return (T) this;
}
示例4: parseSelectionStr
import javax.persistence.criteria.Selection; //导入依赖的package包/类
private void parseSelectionStr(List<Selection<?>> result, String selection) {
String[] ps = selection.split("\\s*,\\s*");
for (String p : ps) {
String alias = p.trim();
String[] pa = alias.split("\\s+[aA][sS]\\s+");
if (pa.length > 1) {
alias = pa[1];
} else {
pa = alias.split("\\s+");
if (pa.length > 1) {
alias = pa[1];
}
}
result.add(root.get(ps[0]).alias(alias));
}
}
示例5: distinct
import javax.persistence.criteria.Selection; //导入依赖的package包/类
/**
* @param distinctFields
* @return
* @throws IllegalArgumentException if the field is not part of the entity
*/
public JpaELFilterImpl distinct(String... distinctFields) {
if (distinctFields != null) {
List<Selection> multiSelection = new ArrayList<>();
for (String f : distinctFields) {
multiSelection.add(resultRoot.get(f));
}
if (distinctFields.length == 1) {
critQ.select(multiSelection.get(0)).distinct(true);
} else if (distinctFields.length > 0) {
critQ.multiselect(multiSelection).distinct(true);
}
}
return this;
}
示例6: sum
import javax.persistence.criteria.Selection; //导入依赖的package包/类
@Override
public <S> S sum(Class<S> resultClass, Specification<E> spec, LockModeType lockMode, List<String> properties) {
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<S> query = builder.createQuery(resultClass);
Root<E> root = query.from(getDomainClass());
List<Selection<?>> selectionList = Lists.newArrayList();
for (String property : properties) {
selectionList.add(builder.sum(getAttributeName(root, property)));
}
return aggregate(builder, query, root, spec, selectionList, lockMode);
}
示例7: addSelection
import javax.persistence.criteria.Selection; //导入依赖的package包/类
@Override
public void addSelection(Expression<?> expression, String name) {
Selection<?> selection = criteriaQuery.getSelection();
List<Selection<?>> newSelection = new ArrayList<>();
if (selection != null) {
if (selection.isCompoundSelection()) {
newSelection.addAll(selection.getCompoundSelectionItems());
}
else {
newSelection.add(selection);
}
}
newSelection.add(expression);
criteriaQuery.multiselect(newSelection);
}
示例8: findByTupleQuery
import javax.persistence.criteria.Selection; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public <T extends BaseEntity> List<Tuple> findByTupleQuery(TupleQueryCriteria<T> criteria) {
EntityManager em = this.emf.createEntityManager();
try {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Tuple> cq = cb.createTupleQuery();
Root<T> root = cq.from(criteria.getEntity());
return em.createQuery(cq.multiselect(criteria.getSelections()
.stream()
.map(root::get)
.collect(Collectors.toList())
.toArray(new Selection[LEN_ZERO]))
.where(cb.and(JpaUtil.getPredicates(criteria.getCriteriaAttributes(), cb, root))))
.getResultList();
} catch (RuntimeException ex) {
LOGGER.error(ex.getMessage(), ex);
throw new PersistenceException(ex.getMessage(), ex);
} finally {
JpaUtil.closeEntityManager(em);
}
}
示例9: visit
import javax.persistence.criteria.Selection; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
public boolean visit(JpqlSubselect node, CriteriaHolder query) {
try {
Subquery<Object> subquery = query.createSubquery();
node.jjtGetChild(1).visit(this, query);
node.jjtGetChild(0).visit(this, query);
List<Selection<?>> selections = query.<List<Selection<?>>>getCurrentValue();
subquery.select((Expression<Object>)selections.iterator().next());
query.setValue(subquery);
for (int i = 2; i < node.jjtGetNumChildren(); i++) {
node.jjtGetChild(i).visit(this, query);
}
query.setValue(subquery);
return false;
} finally {
query.removeSubquery();
}
}
示例10: criteria
import javax.persistence.criteria.Selection; //导入依赖的package包/类
@Test
public void criteria() {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<ClientEntityBrowserDto> c = cb.createQuery(ClientEntityBrowserDto.class);
Root<Client> client = c.from(Client.class);
Join<Client, ClientGroup> clientGroup = client.join("group", JoinType.LEFT);
Join<Client, ClientStructure> clientStructure = client.join("structure", JoinType.LEFT);
Join<ClientType, ClientTypeGroup> clientTypeGroup
= client.join("type", JoinType.LEFT).join("clientTypeGroup", JoinType.LEFT);
Selection<ClientEntityBrowserDto> selection
= cb.construct(ClientEntityBrowserDto.class,
client.<Integer>get("id"),
client.<String>get("number"),
client.<String>get("name"),
clientTypeGroup.<String>get("name"),
clientGroup.<String>get("name"),
clientStructure.<String>get("name"));
c.select(selection);
c.distinct(true);
TypedQuery<ClientEntityBrowserDto> query = entityManager.createQuery(c);
assertTrue(query.getResultList().isEmpty());
}
示例11: checkMultiselect
import javax.persistence.criteria.Selection; //导入依赖的package包/类
/**
* Package-protected method to centralize checking of criteria query multi-selects as defined by the
* {@link CriteriaQuery#multiselect(List)} method.
*
* @param selections The selection varargs to check
*
* @throws IllegalArgumentException If the selection items are not valid per {@link CriteriaQuery#multiselect}
* documentation.
* <i>"An argument to the multiselect method must not be a tuple-
* or array-valued compound selection item."</i>
*/
void checkMultiselect(List<Selection<?>> selections) {
// final HashSet<String> aliases = new HashSet<String>( CollectionHelper.determineProperSizing( selections.size() ) );
//
// for ( Selection<?> selection : selections ) {
// if ( selection.isCompoundSelection() ) {
// if ( selection.getJavaType().isArray() ) {
// throw new IllegalArgumentException(
// "Selection items in a multi-select cannot contain compound array-valued elements"
// );
// }
// if ( Tuple.class.isAssignableFrom( selection.getJavaType() ) ) {
// throw new IllegalArgumentException(
// "Selection items in a multi-select cannot contain compound tuple-valued elements"
// );
// }
// }
// if ( StringHelper.isNotEmpty( selection.getAlias() ) ) {
// boolean added = aliases.add( selection.getAlias() );
// if ( ! added ) {
// throw new IllegalArgumentException( "Multi-select expressions defined duplicate alias : " + selection.getAlias() );
// }
// }
// }
}
示例12: toExpressions
import javax.persistence.criteria.Selection; //导入依赖的package包/类
private List<JpaExpression<?>> toExpressions(List<Selection<?>> selections) {
if ( selections == null || selections.isEmpty() ) {
return Collections.emptyList();
}
final ArrayList<JpaExpression<?>> expressions = new ArrayList<>();
for ( Selection<?> selection : selections ) {
if ( selection instanceof JpaExpression ) {
expressions.add( (JpaExpression) selection );
}
else {
throw new CriteriaBuilderException(
"Expecting javax.persistence.criteria.Selection to be " +
"JpaExpression, but found " +
selection.toString()
);
}
}
return expressions;
}
示例13: prepareProjectingQuery
import javax.persistence.criteria.Selection; //导入依赖的package包/类
public <E> List<Selection<?>> prepareProjectingQuery(MetaJpaConstructor<E,?,?> projection, From<?,? extends E> selection) {
logger.debug("prepareProjectingQuery({},{})", projection, selection);
List<Selection<?>> ret;
if (projection instanceof IdProjection) {
logger.debug("IdProjection. Replacing selection {} with just Id.", selection);
ret = Collections.<Selection<?>>newList(selection.get(QueryUtils.<E,Object>id(selection.getJavaType(), em.apply())));
} else {
ret = newListOfSize(projection.getParameters().size());
for (Tuple3<Integer, Attribute<?,?>, Class<?>> t: zip(range(0), projection.getParameters(), projection.getConstructorParameterTypes())) {
int index = t._1;
Attribute<?,?> param = t._2;
Class<?> constuctorParameterType = t._3;
ret.add(transformSelectionForQuery(param, isId(constuctorParameterType) || isWrapperOfIds(projection, index), selection, projection));
}
}
logger.debug("prepareProjectingQuery -> {}", ret);
return ret;
}
示例14: getCriteriaQuery
import javax.persistence.criteria.Selection; //导入依赖的package包/类
/**
* Create a TypedQuery from a request page
* @param page request page
* @return new TypedQuery
*/
@SuppressWarnings("unchecked")
private <K> TypedQuery<K> getCriteriaQuery(Page<K> page) {
CriteriaQuery<K> criteria = getCriteria(page);
CriteriaQuery<Long> countCriteria = (CriteriaQuery<Long>) getCriteria(page);
CriteriaBuilder cb = em.getCriteriaBuilder();
Root<?> root = countCriteria.getRoots().iterator().next();
countCriteria.select(cb.count(root));
page.setCount((em.createQuery(countCriteria).getSingleResult())
.intValue());
criteria.orderBy(getOrder(page, criteria));
// Add default select to entity class if none was set.
if (criteria.getSelection() == null) {
criteria.select((Selection<? extends K>) root);
}
return em.createQuery(criteria);
}
示例15: sum
import javax.persistence.criteria.Selection; //导入依赖的package包/类
@Override
public <S> S sum(Class<S> resultClass, Specification<E> spec, LockModeType lockMode, List<SingularAttribute<E, ? extends Number>> properties) {
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<S> query = builder.createQuery(resultClass);
Root<E> root = query.from(getDomainClass());
List<Selection<?>> selectionList = Lists.newArrayList();
for (SingularAttribute<E, ? extends Number> property : properties) {
selectionList.add(builder.sum(root.get(property)));
}
return aggregate(builder, query, root, spec, selectionList, lockMode);
}