use of jakarta.persistence.criteria.Selection in project hibernate-orm by hibernate.
the class MultiSelectTests method tupleSelectionArrayTest.
@Test
public void tupleSelectionArrayTest(SessionFactoryScope scope) {
scope.inTransaction((session) -> {
final CriteriaBuilder nodeBuilder = session.getFactory().getNodeBuilder();
final CriteriaQuery<Tuple> criteria = nodeBuilder.createTupleQuery();
final Root<BasicEntity> root = criteria.from(BasicEntity.class);
Selection<?>[] s = { root.get("id"), root.get("data") };
criteria.select(nodeBuilder.tuple(s));
final List<Tuple> results = session.createQuery(criteria).list();
assertThat(results).hasSize(1);
final Tuple firstResult = results.get(0);
assertThat(firstResult.getElements()).hasSize(2);
assertThat(firstResult.get(0)).isEqualTo(1);
assertThat(firstResult.get(1)).isEqualTo("abc");
});
}
use of jakarta.persistence.criteria.Selection in project hibernate-orm by hibernate.
the class AbstractQueryCacheResultTransformerTest method testJoinWithFetchJoinWithOwnerAndAliasedJoinedProjectedList.
@Test
public void testJoinWithFetchJoinWithOwnerAndAliasedJoinedProjectedList(SessionFactoryScope scope) throws Exception {
CriteriaExecutor criteriaExecutor = new CriteriaExecutor() {
@Override
protected ResultTransformer getResultTransformer() {
return null;
}
@Override
protected JpaCriteriaQuery getCriteria(Session s) {
CriteriaBuilder builder = s.getCriteriaBuilder();
JpaCriteriaQuery criteria = (JpaCriteriaQuery) builder.createQuery(Object[].class);
JpaRoot<Student> root = criteria.from(Student.class);
root.fetch("enrolments", JoinType.LEFT);
final Selection<Object> pc = root.join("preferredCourse", JoinType.LEFT).alias("pc");
criteria.orderBy(builder.asc(root.get("studentNumber")));
criteria.multiselect(root, pc);
return criteria;
}
};
HqlExecutor hqlExecutor = new HqlExecutor() {
@Override
public Query getQuery(Session s) {
return s.createQuery("select s, pc from Student s left join fetch s.enrolments left join s.preferredCourse pc order by s.studentNumber");
}
};
ResultChecker checker = results -> {
List resultList = (List) results;
assertEquals(2, resultList.size());
Object[] yogiObjects = (Object[]) resultList.get(0);
assertEquals(yogiExpected, yogiObjects[0]);
assertEquals(yogiExpected.getPreferredCourse().getCourseCode(), ((Course) yogiObjects[1]).getCourseCode());
Object[] shermanObjects = (Object[]) resultList.get(1);
assertEquals(shermanExpected, shermanObjects[0]);
assertNull(shermanObjects[1]);
if (areDynamicNonLazyAssociationsChecked()) {
assertEquals(yogiExpected.getPreferredCourse(), yogiObjects[1]);
assertTrue(Hibernate.isInitialized(((Student) yogiObjects[0]).getEnrolments()));
assertEquals(yogiExpected.getEnrolments(), ((Student) yogiObjects[0]).getEnrolments());
assertTrue(Hibernate.isInitialized(((Student) shermanObjects[0]).getEnrolments()));
assertEquals(shermanExpected.getEnrolments(), (((Student) shermanObjects[0]).getEnrolments()));
}
};
runTest(hqlExecutor, criteriaExecutor, checker, false, scope);
}
use of jakarta.persistence.criteria.Selection in project eclipselink by eclipse-ee4j.
the class JUnitCriteriaSimpleTestSuiteBase method testCompoundSelectionAliasValidation.
// 366386 - IllegalArgumentException for duplicated aliases
public void testCompoundSelectionAliasValidation() {
EntityManager em = createEntityManager();
CriteriaBuilder qb = em.getCriteriaBuilder();
CriteriaQuery<Tuple> criteria = qb.createTupleQuery();
Root<Employee> emp = wrapper.from(criteria, Employee.class);
Selection[] s = { wrapper.get(emp, Employee_lastName).alias("duplicateAlias"), wrapper.get(emp, Employee_firstName).alias("duplicateAlias") };
try {
criteria.multiselect(s);
fail("IllegalArgumentException expected using multiselect on items using duplicate aliases");
} catch (Exception iae) {
assertEquals(iae.getClass(), IllegalArgumentException.class);
}
Selection<Tuple> tupleItem = qb.tuple(s);
try {
criteria.select(tupleItem);
fail("IllegalArgumentException expected on select using a Tuple with items using duplicate aliases");
} catch (Exception iae) {
assertEquals(iae.getClass(), IllegalArgumentException.class);
}
closeEntityManager(em);
}
use of jakarta.persistence.criteria.Selection in project eclipselink by eclipse-ee4j.
the class CriteriaQueryImpl 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 CriteriaQuery<T> select(Selection<? extends T> selection) {
this.selection = (SelectionImpl<? extends T>) selection;
this.selection.findRootAndParameters(this);
if (selection.isCompoundSelection()) {
// bug 366386: validate that aliases are not reused
if (this.selection.isCompoundSelection() && ((CompoundSelectionImpl) this.selection).getDuplicateAliasNames() != null) {
throw new IllegalArgumentException(ExceptionLocalization.buildMessage("jpa_criteriaapi_alias_reused", new Object[] { ((CompoundSelectionImpl) this.selection).getDuplicateAliasNames() }));
}
if (selection.getJavaType().equals(Tuple.class)) {
this.queryResult = ResultType.TUPLE;
this.queryType = (Class<T>) Tuple.class;
} else if (((InternalSelection) selection).isConstructor()) {
Selection[] selectArray = selection.getCompoundSelectionItems().toArray(new Selection[selection.getCompoundSelectionItems().size()]);
populateAndSetConstructorSelection((ConstructorSelectionImpl) selection, this.selection.getJavaType(), selectArray);
this.queryType = (Class<T>) selection.getJavaType();
} else {
this.queryResult = ResultType.OBJECT_ARRAY;
this.queryType = (Class<T>) ClassConstants.AOBJECT;
}
} else {
// Update query type only when it's not null in selection argument.
Class<?> queryType = selection.getJavaType();
if (queryType != null) {
this.queryType = (Class<T>) queryType;
}
TypeImpl type = ((MetamodelImpl) this.metamodel).getType(this.queryType);
if (type != null && type.getPersistenceType().equals(PersistenceType.ENTITY)) {
// this will be a selection item in a report query
this.queryResult = ResultType.ENTITY;
} else {
this.queryResult = ResultType.OTHER;
}
}
return this;
}
use of jakarta.persistence.criteria.Selection in project eclipselink by eclipse-ee4j.
the class CriteriaQueryImpl method populateAndSetConstructorSelection.
/**
* This method will set this queryImpl's selection to a ConstructorSelectionImpl, creating a new
* instance or populating the one passed in as necessary.
* Throws IllegalArgumentException if a constructor taking arguments represented
* by the selections array doesn't exist for the given class.
*
* Also sets the query result to ResultType.CONSTRUCTOR
*/
public void populateAndSetConstructorSelection(ConstructorSelectionImpl constructorSelection, Class<?> class1, Selection<?>... selections) throws IllegalArgumentException {
Class<?>[] constructorArgs = new Class<?>[selections.length];
int count = 0;
for (Selection select : selections) {
if (select instanceof ConstructorSelectionImpl) {
ConstructorSelectionImpl constructorSelect = (ConstructorSelectionImpl) select;
Selection[] selectArray = constructorSelect.getCompoundSelectionItems().toArray(new Selection[constructorSelect.getCompoundSelectionItems().size()]);
populateAndSetConstructorSelection(constructorSelect, constructorSelect.getJavaType(), selectArray);
}
constructorArgs[count++] = select.getJavaType();
}
Constructor constructor = null;
try {
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
constructor = AccessController.doPrivileged(new PrivilegedGetConstructorFor<>(class1, constructorArgs, false));
} else {
constructor = PrivilegedAccessHelper.getConstructorFor(class1, constructorArgs, false);
}
if (constructorSelection == null) {
constructorSelection = new ConstructorSelectionImpl(class1, selections);
}
this.queryResult = ResultType.CONSTRUCTOR;
constructorSelection.setConstructor(constructor);
constructorSelection.setConstructorArgTypes(constructorArgs);
this.selection = constructorSelection;
} catch (Exception e) {
// PrivilegedActionException and NoSuchMethodException are possible
Object[] params = new Object[1];
params[0] = this.queryType;
throw new IllegalArgumentException(ExceptionLocalization.buildMessage("criteria_no_constructor_found", params), e);
}
}
Aggregations