use of javax.persistence.criteria.Selection in project hibernate-orm by hibernate.
the class TreatListJoinTest method testTreatJoin.
@Test
public void testTreatJoin() {
EntityManager em = createEntityManager();
try {
final CriteriaBuilder cb = em.getCriteriaBuilder();
final CriteriaQuery<Tuple> query = cb.createTupleQuery();
final Root<TestEntity> testEntity = query.from(TestEntity.class);
final List<Selection<?>> selections = new LinkedList();
selections.add(testEntity.get("id"));
final ListJoin<TestEntity, AbstractEntity> entities = testEntity.joinList("entities", JoinType.LEFT);
entities.on(cb.equal(entities.get("entityType"), EntityA.class.getName()));
final ListJoin<TestEntity, EntityA> joinEntityA = cb.treat(entities, EntityA.class);
selections.add(joinEntityA.get("id"));
selections.add(joinEntityA.get("valueA"));
final ListJoin<TestEntity, AbstractEntity> entitiesB = testEntity.joinList("entities", JoinType.LEFT);
entitiesB.on(cb.equal(entitiesB.get("entityType"), EntityB.class.getName()));
final ListJoin<TestEntity, EntityB> joinEntityB = cb.treat(entitiesB, EntityB.class);
selections.add(joinEntityB.get("id"));
selections.add(joinEntityB.get("valueB"));
query.multiselect(selections);
final List<Tuple> resultList = em.createQuery(query).getResultList();
assertThat(resultList.size(), is(10));
} finally {
em.close();
}
}
use of javax.persistence.criteria.Selection in project hibernate-orm by hibernate.
the class CompoundSelectionImpl method getValueHandlers.
@Override
public List<ValueHandlerFactory.ValueHandler> getValueHandlers() {
if (isConstructor) {
return null;
}
boolean foundHandlers = false;
ArrayList<ValueHandlerFactory.ValueHandler> valueHandlers = new ArrayList<ValueHandlerFactory.ValueHandler>();
for (Selection selection : getCompoundSelectionItems()) {
ValueHandlerFactory.ValueHandler valueHandler = ((TupleElementImplementor) selection).getValueHandler();
valueHandlers.add(valueHandler);
foundHandlers = foundHandlers || valueHandler != null;
}
return foundHandlers ? null : valueHandlers;
}
use of javax.persistence.criteria.Selection in project stdlib by petergeneric.
the class JPAQueryBuilder method createSelectIDs.
public Query createSelectIDs() {
this.generated.distinct(true);
// Make sure we return the results in the correct order
generated.orderBy(orders);
List<Selection<?>> selects = new ArrayList<>();
if (root.getModel().hasSingleIdAttribute()) {
selects.add(root.get(root.getModel().getId(root.getModel().getIdType().getJavaType())));
} else
throw new NotImplementedException("Cannot handle ID selection with IdClass!");
for (Order order : orders) {
selects.add(order.getExpression());
}
if (selects.size() == 1)
generated.select(selects.get(0));
else
generated.multiselect(selects);
final Query query = session.createQuery(generated);
if (offset != null)
query.getQueryOptions().setFirstRow(offset);
if (limit != null)
query.getQueryOptions().setMaxRows(limit);
// Set all the parameters
for (Map.Entry<ParameterExpression, Object> entry : this.params.entrySet()) {
query.setParameter(entry.getKey(), entry.getValue());
}
return query;
}
use of javax.persistence.criteria.Selection in project deltaspike by apache.
the class QueryCriteria method createQuery.
@Override
public TypedQuery<R> createQuery() {
try {
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<R> query = createCriteriaQuery(builder);
From<C, C> root = query.from(entityClass);
if (selections.size() == 1) {
Selection<?>[] selections = prepareSelections(query, builder, root);
query.select((Selection<? extends R>) selections[0]);
}
if (selections.size() > 1) {
query.multiselect(prepareSelections(query, builder, root));
}
List<Predicate> predicates = predicates(builder, root);
query.distinct(distinct);
if (!predicates.isEmpty()) {
query.where(predicates.toArray(new Predicate[predicates.size()]));
}
applyProcessors(query, builder, root);
return (TypedQuery<R>) entityManager.createQuery(query);
} catch (RuntimeException e) {
log.log(Level.SEVERE, "Exception while creating JPA query", e);
throw e;
}
}
use of javax.persistence.criteria.Selection in project hibernate-orm by hibernate.
the class CompoundSelectionImpl method render.
public String render(RenderingContext renderingContext) {
StringBuilder buff = new StringBuilder();
if (isConstructor) {
buff.append("new ").append(getJavaType().getName()).append('(');
}
String sep = "";
for (Selection selection : selectionItems) {
buff.append(sep).append(((Renderable) selection).renderProjection(renderingContext));
sep = ", ";
}
if (isConstructor) {
buff.append(')');
}
return buff.toString();
}
Aggregations