Search in sources :

Example 16 with SqmPath

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;
}
Also used : VirtualTableGroup(org.hibernate.sql.ast.tree.from.VirtualTableGroup) LazyTableGroup(org.hibernate.sql.ast.tree.from.LazyTableGroup) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) CorrelatedTableGroup(org.hibernate.sql.ast.tree.from.CorrelatedTableGroup) CorrelatedPluralTableGroup(org.hibernate.sql.ast.tree.from.CorrelatedPluralTableGroup) PluralTableGroup(org.hibernate.sql.ast.tree.from.PluralTableGroup) QueryPartTableGroup(org.hibernate.sql.ast.tree.from.QueryPartTableGroup) SqmFrom(org.hibernate.query.sqm.tree.from.SqmFrom) SqlTreeCreationException(org.hibernate.sql.ast.SqlTreeCreationException) SqmTreatedPath(org.hibernate.query.sqm.tree.domain.SqmTreatedPath) SelfInterpretingSqmPath(org.hibernate.query.sqm.sql.internal.SelfInterpretingSqmPath) SqmPath(org.hibernate.query.sqm.tree.domain.SqmPath) DiscriminatorSqmPath(org.hibernate.metamodel.model.domain.internal.DiscriminatorSqmPath)

Example 17 with SqmPath

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();
    }
}
Also used : SqmSelection(org.hibernate.query.sqm.tree.select.SqmSelection) SqmValues(org.hibernate.query.sqm.tree.insert.SqmValues) SqmInsertValuesStatement(org.hibernate.query.sqm.tree.insert.SqmInsertValuesStatement) SqmPath(org.hibernate.query.sqm.tree.domain.SqmPath) SqmInsertSelectStatement(org.hibernate.query.sqm.tree.insert.SqmInsertSelectStatement)

Example 18 with SqmPath

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");
}
Also used : SqmSelection(org.hibernate.query.sqm.tree.select.SqmSelection) SqmComparisonPredicate(org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate) SqmEntityValuedSimplePath(org.hibernate.query.sqm.tree.domain.SqmEntityValuedSimplePath) SqmPath(org.hibernate.query.sqm.tree.domain.SqmPath) EntityDomainType(org.hibernate.metamodel.model.domain.EntityDomainType) BaseSqmUnitTest(org.hibernate.orm.test.query.sqm.BaseSqmUnitTest) Test(org.junit.jupiter.api.Test)

Example 19 with SqmPath

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();
    });
}
Also used : SqmPath(org.hibernate.query.sqm.tree.domain.SqmPath) HibernateCriteriaBuilder(org.hibernate.query.criteria.HibernateCriteriaBuilder) BaseSessionFactoryFunctionalTest(org.hibernate.testing.orm.junit.BaseSessionFactoryFunctionalTest) Test(org.junit.jupiter.api.Test) RequiresDialectFeature(org.hibernate.testing.orm.junit.RequiresDialectFeature)

Example 20 with SqmPath

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"));
}
Also used : SqmPredicate(org.hibernate.query.sqm.tree.predicate.SqmPredicate) SqmComparisonPredicate(org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate) SqmPath(org.hibernate.query.sqm.tree.domain.SqmPath) SqmLiteral(org.hibernate.query.sqm.tree.expression.SqmLiteral) BaseSqmUnitTest(org.hibernate.orm.test.query.sqm.BaseSqmUnitTest) Test(org.junit.jupiter.api.Test)

Aggregations

SqmPath (org.hibernate.query.sqm.tree.domain.SqmPath)21 DiscriminatorSqmPath (org.hibernate.metamodel.model.domain.internal.DiscriminatorSqmPath)10 SelfInterpretingSqmPath (org.hibernate.query.sqm.sql.internal.SelfInterpretingSqmPath)10 SqmComparisonPredicate (org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate)6 HashSet (java.util.HashSet)5 Set (java.util.Set)5 SqmPredicate (org.hibernate.query.sqm.tree.predicate.SqmPredicate)5 SqmSelection (org.hibernate.query.sqm.tree.select.SqmSelection)5 AbstractMap (java.util.AbstractMap)4 HashMap (java.util.HashMap)4 IdentityHashMap (java.util.IdentityHashMap)4 LinkedHashMap (java.util.LinkedHashMap)4 Map (java.util.Map)4 SqmLiteral (org.hibernate.query.sqm.tree.expression.SqmLiteral)4 SqmAndPredicate (org.hibernate.query.sqm.tree.predicate.SqmAndPredicate)4 SqmBetweenPredicate (org.hibernate.query.sqm.tree.predicate.SqmBetweenPredicate)4 SqmBooleanExpressionPredicate (org.hibernate.query.sqm.tree.predicate.SqmBooleanExpressionPredicate)4 SqmEmptinessPredicate (org.hibernate.query.sqm.tree.predicate.SqmEmptinessPredicate)4 SqmExistsPredicate (org.hibernate.query.sqm.tree.predicate.SqmExistsPredicate)4 SqmGroupedPredicate (org.hibernate.query.sqm.tree.predicate.SqmGroupedPredicate)4