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);
}
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);
}
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;
}
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();
}
}
}
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;
}
Aggregations