Search in sources :

Example 6 with Selection

use of jakarta.persistence.criteria.Selection in project eclipselink by eclipse-ee4j.

the class JUnitCriteriaSimpleTestSuiteBase method testCriteriaBuilderTupleValidation.

// 366199 - CriteriaBuilder.tuple(Selection) does not throw IllegalArgumentException
public void testCriteriaBuilderTupleValidation() {
    EntityManager em = createEntityManager();
    CriteriaBuilder qb = em.getCriteriaBuilder();
    CriteriaQuery<Tuple> cquery = qb.createTupleQuery();
    Root<Employee> emp = wrapper.from(cquery, Employee.class);
    Selection[] s = { wrapper.get(emp, Employee_id), wrapper.get(emp, Employee_lastName), wrapper.get(emp, Employee_firstName) };
    Selection<Tuple> item = qb.tuple(s);
    cquery.select(item);
    TypedQuery<Tuple> query = em.createQuery(cquery);
    // verify they work and can be used:
    List<Tuple> list = query.getResultList();
    list.get(0);
    try {
        // verify tuple throws an exception when passed a tuple
        Object unexpectedResult = qb.tuple(item);
        fail("IllegalArgumentException expected using an invalid value to CriteriaBuilder.tuple(). Result returned:" + unexpectedResult);
    } catch (Exception iae) {
        assertEquals(iae.getClass(), IllegalArgumentException.class);
    }
    try {
        // verify array throws an exception when passed a tuple
        Object unexpectedResult = qb.array(item);
        fail("IllegalArgumentException expected using an invalid value to CriteriaBuilder.array(). Result returned:" + unexpectedResult);
    } catch (Exception iae) {
        assertEquals(iae.getClass(), IllegalArgumentException.class);
    }
    closeEntityManager(em);
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) Selection(jakarta.persistence.criteria.Selection) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Tuple(jakarta.persistence.Tuple)

Example 7 with Selection

use of jakarta.persistence.criteria.Selection in project eclipselink by eclipse-ee4j.

the class JUnitCriteriaSimpleTestSuiteBase method testCriteriaBuilderConstructValidation.

// 366248 - CriteriaBuilder.construct(Class, Selection)
public void testCriteriaBuilderConstructValidation() {
    EntityManager em = createEntityManager();
    Employee emp = (Employee) getServerSession().readAllObjects(Employee.class).firstElement();
    EmployeeDetail expectedResult = new EmployeeDetail(emp.getFirstName(), emp.getLastName());
    CriteriaBuilder qb = em.getCriteriaBuilder();
    CriteriaQuery<EmployeeDetail> cquery = qb.createQuery(EmployeeDetail.class);
    Root<Employee> root = wrapper.from(cquery, Employee.class);
    Selection[] s = { wrapper.get(root, Employee_firstName), wrapper.get(root, Employee_lastName) };
    Selection<EmployeeDetail> item = qb.construct(EmployeeDetail.class, s);
    cquery.select(item);
    cquery.where(qb.equal(root.get("id"), emp.getId()));
    TypedQuery<EmployeeDetail> query = em.createQuery(cquery);
    // verify the query works:
    List<EmployeeDetail> list = query.getResultList();
    EmployeeDetail result = list.get(0);
    assertEquals("Constructor criteria query Failed", expectedResult, result);
    try {
        // verify construct throws an exception when passed an array
        Object unexpectedResult = qb.construct(EmployeeDetail.class, qb.array(s));
        fail("IllegalArgumentException expected using an invalid value to CriteriaBuilder.tuple(). Result returned:" + unexpectedResult);
    } catch (Exception iae) {
        assertEquals(iae.getClass(), IllegalArgumentException.class);
    }
    try {
        // verify construct throws an exception when passed a tuple
        Object unexpectedResult = qb.construct(EmployeeDetail.class, qb.tuple(s));
        fail("IllegalArgumentException expected using an invalid value to CriteriaBuilder.tuple(). Result returned:" + unexpectedResult);
    } catch (Exception iae) {
        assertEquals(iae.getClass(), IllegalArgumentException.class);
    }
    closeEntityManager(em);
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) Selection(jakarta.persistence.criteria.Selection) EmployeeDetail(org.eclipse.persistence.testing.tests.jpa.jpql.JUnitJPQLComplexTestSuite.EmployeeDetail) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee)

Example 8 with Selection

use of jakarta.persistence.criteria.Selection in project eclipselink by eclipse-ee4j.

the class ConstructorSelectionImpl method translate.

public ConstructorReportItem translate() {
    ConstructorReportItem item = new ConstructorReportItem(this.getAlias());
    item.setResultType(this.getJavaType());
    item.setConstructor(constructor);
    for (Selection selection : this.getCompoundSelectionItems()) {
        if (selection.isCompoundSelection()) {
            item.addItem(((ConstructorSelectionImpl) selection).translate());
        } else {
            ReportItem reportItem = new ReportItem(item.getName() + item.getReportItems().size(), ((SelectionImpl) selection).getCurrentNode());
            // bug: 297385 - set type here because the selection already knows the type
            reportItem.setResultType(selection.getJavaType());
            item.addItem(reportItem);
        }
    }
    return item;
}
Also used : ConstructorReportItem(org.eclipse.persistence.queries.ConstructorReportItem) Selection(jakarta.persistence.criteria.Selection) ConstructorReportItem(org.eclipse.persistence.queries.ConstructorReportItem) ReportItem(org.eclipse.persistence.internal.queries.ReportItem)

Example 9 with Selection

use of jakarta.persistence.criteria.Selection in project eclipselink by eclipse-ee4j.

the class TestNegFunction method testNegFunction.

@Test
public void testNegFunction() throws Exception {
    EntityManager em = emf.createEntityManager();
    try {
        CritEntity ce = new CritEntity();
        ce.setId(1);
        ce.setValue(new BigDecimal("3.14"));
        em.getTransaction().begin();
        em.persist(ce);
        ;
        em.getTransaction().commit();
        em.clear();
        CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
        CriteriaQuery<DecHolder> criteriaQuery = criteriaBuilder.createQuery(DecHolder.class);
        Root<CritEntity> entityRoot = criteriaQuery.from(CritEntity.class);
        Collection<Selection<?>> selections = new ArrayList<Selection<?>>();
        Expression<BigDecimal> valExpr = entityRoot.get("value");
        selections.add(criteriaBuilder.sum(criteriaBuilder.<BigDecimal>selectCase().when(criteriaBuilder.equal(entityRoot.get("id"), 0), valExpr).otherwise(criteriaBuilder.neg(valExpr))));
        criteriaQuery.multiselect(selections.toArray(new Selection[] {}));
        List<DecHolder> retList = em.createQuery(criteriaQuery).getResultList();
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        if (em.isOpen()) {
            em.close();
        }
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) EntityManager(jakarta.persistence.EntityManager) Selection(jakarta.persistence.criteria.Selection) ArrayList(java.util.ArrayList) CritEntity(org.eclipse.persistence.jpa.test.criteria.model.CritEntity) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 10 with Selection

use of jakarta.persistence.criteria.Selection in project eclipselink by eclipse-ee4j.

the class SubQueryImpl method select.

/**
 * Specify the item that is to be returned in the query result.
 * Replaces the previously specified selection, if any.
 * @param selection  selection specifying the item that
 *        is to be returned in the query result
 * @return the modified query
 */
@Override
public Subquery<T> select(Expression<T> selection) {
    findRootAndParameters(selection);
    for (Iterator<Root<?>> iterator = this.getRoots().iterator(); iterator.hasNext(); ) {
        findJoins((FromImpl) iterator.next());
    }
    for (Iterator<Join<?, ?>> iterator = this.getCorrelatedJoins().iterator(); iterator.hasNext(); ) {
        findJoins((FromImpl) iterator.next());
    }
    this.selection = (SelectionImpl) selection;
    this.queryType = (Class<T>) selection.getJavaType();
    this.subQuery.getItems().clear();
    if (selection.isCompoundSelection()) {
        int count = 0;
        for (Selection select : selection.getCompoundSelectionItems()) {
            this.subQuery.addItem(String.valueOf(count), ((InternalSelection) select).getCurrentNode());
            ++count;
        }
        this.subQuery.setExpressionBuilder(((InternalSelection) selection.getCompoundSelectionItems().get(0)).getCurrentNode().getBuilder());
    } else {
        TypeImpl<? extends T> type = ((MetamodelImpl) this.metamodel).getType(selection.getJavaType());
        if (type != null && type.getPersistenceType().equals(PersistenceType.ENTITY)) {
            this.subQuery.addAttribute("", new ConstantExpression(1, ((InternalSelection) selection).getCurrentNode().getBuilder()));
            this.subQuery.addNonFetchJoinedAttribute(((InternalSelection) selection).getCurrentNode());
        } else {
            String itemName = selection.getAlias();
            if (itemName == null) {
                itemName = ((InternalSelection) selection).getCurrentNode().getName();
            }
            this.subQuery.addItem(itemName, ((InternalSelection) selection).getCurrentNode());
        }
        this.subQuery.setExpressionBuilder(((InternalSelection) selection).getCurrentNode().getBuilder());
    }
    return this;
}
Also used : MetamodelImpl(org.eclipse.persistence.internal.jpa.metamodel.MetamodelImpl) Root(jakarta.persistence.criteria.Root) Selection(jakarta.persistence.criteria.Selection) ConstantExpression(org.eclipse.persistence.internal.expressions.ConstantExpression) CollectionJoin(jakarta.persistence.criteria.CollectionJoin) ListJoin(jakarta.persistence.criteria.ListJoin) SetJoin(jakarta.persistence.criteria.SetJoin) MapJoin(jakarta.persistence.criteria.MapJoin) Join(jakarta.persistence.criteria.Join)

Aggregations

Selection (jakarta.persistence.criteria.Selection)17 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)11 EntityManager (jakarta.persistence.EntityManager)6 Join (jakarta.persistence.criteria.Join)5 ListJoin (jakarta.persistence.criteria.ListJoin)5 MapJoin (jakarta.persistence.criteria.MapJoin)5 Root (jakarta.persistence.criteria.Root)5 Constructor (java.lang.reflect.Constructor)5 ArrayList (java.util.ArrayList)5 Tuple (jakarta.persistence.Tuple)4 JoinType (jakarta.persistence.criteria.JoinType)4 Order (jakarta.persistence.criteria.Order)4 Path (jakarta.persistence.criteria.Path)4 Iterator (java.util.Iterator)4 List (java.util.List)4 Map (java.util.Map)4 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)4 Employee (org.eclipse.persistence.testing.models.jpa.advanced.Employee)4 CacheMode (org.hibernate.CacheMode)4 Hibernate (org.hibernate.Hibernate)4