Search in sources :

Example 1 with Selection

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();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Selection(javax.persistence.criteria.Selection) LinkedList(java.util.LinkedList) EntityManager(javax.persistence.EntityManager) Tuple(javax.persistence.Tuple) Test(org.junit.Test)

Example 2 with Selection

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;
}
Also used : TupleElementImplementor(org.hibernate.query.criteria.internal.TupleElementImplementor) CompoundSelection(javax.persistence.criteria.CompoundSelection) Selection(javax.persistence.criteria.Selection) ArrayList(java.util.ArrayList) ValueHandlerFactory(org.hibernate.query.criteria.internal.ValueHandlerFactory)

Example 3 with Selection

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;
}
Also used : WQOrder(com.peterphi.std.guice.restclient.jaxb.webquery.WQOrder) Order(javax.persistence.criteria.Order) Query(org.hibernate.query.Query) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) WebQuery(com.peterphi.std.guice.restclient.jaxb.webquery.WebQuery) Selection(javax.persistence.criteria.Selection) NotImplementedException(com.peterphi.std.NotImplementedException) ParameterExpression(javax.persistence.criteria.ParameterExpression) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with Selection

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;
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) TypedQuery(javax.persistence.TypedQuery) Selection(javax.persistence.criteria.Selection) QuerySelection(org.apache.deltaspike.data.api.criteria.QuerySelection) Predicate(javax.persistence.criteria.Predicate)

Example 5 with Selection

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();
}
Also used : CompoundSelection(javax.persistence.criteria.CompoundSelection) Selection(javax.persistence.criteria.Selection)

Aggregations

Selection (javax.persistence.criteria.Selection)8 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)4 ArrayList (java.util.ArrayList)3 EntityManager (javax.persistence.EntityManager)2 CompoundSelection (javax.persistence.criteria.CompoundSelection)2 Predicate (javax.persistence.criteria.Predicate)2 PersistenceException (com.adeptj.modules.data.jpa.PersistenceException)1 NotImplementedException (com.peterphi.std.NotImplementedException)1 WQOrder (com.peterphi.std.guice.restclient.jaxb.webquery.WQOrder)1 WebQuery (com.peterphi.std.guice.restclient.jaxb.webquery.WebQuery)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 Tuple (javax.persistence.Tuple)1 TypedQuery (javax.persistence.TypedQuery)1 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)1 Expression (javax.persistence.criteria.Expression)1 Order (javax.persistence.criteria.Order)1 ParameterExpression (javax.persistence.criteria.ParameterExpression)1 QuerySelection (org.apache.deltaspike.data.api.criteria.QuerySelection)1