use of org.hibernate.query.sqm.tree.domain.SqmPath in project hibernate-orm by hibernate.
the class BaseSqmToSqlAstConverter method prepareReusablePath.
private TableGroup prepareReusablePath(FromClauseIndex fromClauseIndex, JpaPath<?> sqmPath, Consumer<TableGroup> implicitJoinChecker) {
final JpaPath<?> parentPath;
if (sqmPath instanceof SqmTreatedPath<?, ?>) {
parentPath = ((SqmTreatedPath<?, ?>) sqmPath).getWrappedPath();
} else {
parentPath = sqmPath.getParentPath();
}
if (parentPath == null) {
return null;
}
final TableGroup tableGroup = fromClauseIndex.findTableGroup(parentPath.getNavigablePath());
if (tableGroup == null) {
final TableGroup parentTableGroup = prepareReusablePath(fromClauseIndex, parentPath, implicitJoinChecker);
if (parentTableGroup == null) {
final TableGroup parent = fromClauseIndex.findTableGroupOnParents(parentPath.getNavigablePath());
if (parent != null) {
throw new SqlTreeCreationException("Found un-correlated path usage in sub query - " + parentPath);
}
throw new SqlTreeCreationException("Could not locate TableGroup - " + parentPath.getNavigablePath());
}
if (parentPath instanceof SqmTreatedPath<?, ?>) {
fromClauseIndex.register((SqmPath<?>) parentPath, parentTableGroup);
return parentTableGroup;
}
final TableGroup newTableGroup = createTableGroup(parentTableGroup, (SqmPath<?>) parentPath);
if (newTableGroup != null) {
implicitJoinChecker.accept(newTableGroup);
if (sqmPath instanceof SqmFrom<?, ?>) {
registerTreatUsage((SqmFrom<?, ?>) sqmPath, newTableGroup);
}
}
return newTableGroup;
} else if (sqmPath instanceof SqmTreatedPath<?, ?>) {
fromClauseIndex.register((SqmPath<?>) sqmPath, tableGroup);
if (sqmPath instanceof SqmFrom<?, ?>) {
registerTreatUsage((SqmFrom<?, ?>) sqmPath, tableGroup);
}
} else if (parentPath instanceof SqmFrom<?, ?>) {
registerTreatUsage((SqmFrom<?, ?>) parentPath, tableGroup);
}
return tableGroup;
}
use of org.hibernate.query.sqm.tree.domain.SqmPath in project hibernate-orm by hibernate.
the class QuerySqmImpl method verifyInsertTypesMatch.
private void verifyInsertTypesMatch(String hqlString, SqmInsertStatement<R> sqmStatement) {
final List<SqmPath<?>> insertionTargetPaths = sqmStatement.getInsertionTargetPaths();
if (sqmStatement instanceof SqmInsertValuesStatement<?>) {
final SqmInsertValuesStatement<R> statement = (SqmInsertValuesStatement<R>) sqmStatement;
for (SqmValues sqmValues : statement.getValuesList()) {
verifyInsertTypesMatch(hqlString, insertionTargetPaths, sqmValues.getExpressions());
}
} else {
final SqmInsertSelectStatement<R> statement = (SqmInsertSelectStatement<R>) sqmStatement;
final List<SqmSelection<?>> selections = statement.getSelectQueryPart().getFirstQuerySpec().getSelectClause().getSelections();
verifyInsertTypesMatch(hqlString, insertionTargetPaths, selections);
statement.getSelectQueryPart().validateQueryStructureAndFetchOwners();
}
}
use of org.hibernate.query.sqm.tree.domain.SqmPath in project hibernate-orm by hibernate.
the class AttributePathTests method testImplicitJoinReuse2.
@Test
public void testImplicitJoinReuse2() {
final SqmSelectStatement<?> statement = interpretSelect("select s.mate from Person s where s.mate.dob = ?1");
assertThat(statement.getQuerySpec().getFromClause().getRoots().size(), is(1));
final SqmRoot<?> sqmRoot = statement.getQuerySpec().getFromClause().getRoots().get(0);
assertThat(sqmRoot.getJoins().size(), is(0));
assertThat(sqmRoot.getReusablePaths().size(), is(1));
final SqmSelection selection = statement.getQuerySpec().getSelectClause().getSelections().get(0);
assertThat(selection.getSelectableNode(), instanceOf(SqmEntityValuedSimplePath.class));
final SqmPath selectExpression = (SqmPath) selection.getSelectableNode();
assertThat(selectExpression.getReferencedPathSource().getSqmPathType(), instanceOf(EntityDomainType.class));
final SqmComparisonPredicate predicate = (SqmComparisonPredicate) statement.getQuerySpec().getWhereClause().getPredicate();
final SqmPath predicateLhs = (SqmPath) predicate.getLeftHandExpression();
assertThat(predicateLhs.getLhs(), notNullValue());
// from-clause paths
// assertPropertyPath( space.getRoot(), "com.acme.Something(s)" );
// assertPropertyPath( space.getJoins().get( 0 ), "com.acme.Something(s).entity" );
// expression paths
assertPropertyPath((SqmExpression) selection.getSelectableNode(), Person.class.getName() + "(s).mate");
assertPropertyPath(predicateLhs, Person.class.getName() + "(s).mate.dob");
}
use of org.hibernate.query.sqm.tree.domain.SqmPath in project hibernate-orm by hibernate.
the class LockModeTest method testCriteriaAliasSpecific.
@Test
@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsLockTimeouts.class)
public void testCriteriaAliasSpecific() {
// open a session, begin a transaction and lock row
doInHibernate(this::sessionFactory, session -> {
HibernateCriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
JpaCriteriaQuery<A> criteria = criteriaBuilder.createQuery(A.class);
((SqmPath<?>) criteria.from(A.class)).setExplicitAlias("this");
A it = session.createQuery(criteria).setLockMode("this", LockMode.PESSIMISTIC_WRITE).uniqueResult();
// A it = (A) session.createCriteria( A.class )
// .setLockMode( "this", LockMode.PESSIMISTIC_WRITE )
// .uniqueResult();
// make sure we got it
assertNotNull(it);
// that initial transaction is still active and so the lock should still be held.
// Lets open another session/transaction and verify that we cannot update the row
nowAttemptToUpdateRow();
});
}
use of org.hibernate.query.sqm.tree.domain.SqmPath in project hibernate-orm by hibernate.
the class WhereClauseTests method testListIndexFunction.
@Test
public void testListIndexFunction() {
SqmSelectStatement<?> statement = interpretSelect("select l from EntityOfLists t join t.listOfBasics l where index(l) > 2");
SqmPredicate predicate = statement.getQuerySpec().getWhereClause().getPredicate();
assertThat(predicate, instanceOf(SqmComparisonPredicate.class));
SqmComparisonPredicate relationalPredicate = ((SqmComparisonPredicate) predicate);
assertThat(relationalPredicate.getSqmOperator(), is(ComparisonOperator.GREATER_THAN));
assertThat(relationalPredicate.getRightHandExpression(), instanceOf(SqmLiteral.class));
assertThat(((SqmLiteral<?>) relationalPredicate.getRightHandExpression()).getLiteralValue(), is(2));
assertThat(relationalPredicate.getLeftHandExpression(), instanceOf(SqmPath.class));
final SqmPath<?> indexPath = (SqmPath<?>) relationalPredicate.getLeftHandExpression();
assertThat(indexPath.getLhs(), notNullValue());
assertThat(indexPath.getLhs().getExplicitAlias(), is("l"));
}
Aggregations