Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public interface EntityVersionMapping extends BasicValuedModelPart {

String VERSION_ROLE_NAME = "{version}";

static boolean matchesRoleName(String name) {
return VERSION_ROLE_NAME.equals( name );
}

/**
* The attribute marked as the version
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public <Y> SingularPersistentAttribute<? super J, Y> getVersion(Class<Y> javaTyp
}

@Override
public SingularPersistentAttribute<? super J, ?> findVersionAttribute() {
public SqmSingularPersistentAttribute<? super J, ?> findVersionAttribute() {
if ( versionAttribute != null ) {
return versionAttribute;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.hibernate.metamodel.mapping.DiscriminatorType;
import org.hibernate.metamodel.mapping.EntityDiscriminatorMapping;
import org.hibernate.metamodel.mapping.EntityIdentifierMapping;
import org.hibernate.metamodel.mapping.EntityVersionMapping;
import org.hibernate.metamodel.model.domain.IdentifiableDomainType;
import org.hibernate.metamodel.model.domain.JpaMetamodel;
import org.hibernate.metamodel.model.domain.PersistentAttribute;
Expand All @@ -27,7 +28,6 @@
import org.hibernate.query.sqm.tree.domain.SqmManagedDomainType;
import org.hibernate.query.sqm.tree.domain.SqmPath;
import org.hibernate.query.sqm.tree.domain.SqmPersistentAttribute;
import org.hibernate.query.sqm.tree.domain.SqmSingularPersistentAttribute;
import org.hibernate.query.sqm.tree.domain.SqmEntityDomainType;
import org.hibernate.type.descriptor.java.JavaType;

Expand Down Expand Up @@ -156,6 +156,9 @@ public SqmPathSource<?> findSubPathSource(String name) {
else if ( EntityIdentifierMapping.matchesRoleName( name ) ) {
return hasSingleIdAttribute() ? findIdAttribute() : getIdentifierDescriptor();
}
else if ( EntityVersionMapping.matchesRoleName( name ) ) {
return hasVersionAttribute() ? findVersionAttribute() : null;
}
else if ( EntityDiscriminatorMapping.matchesRoleName( name ) ) {
return discriminatorPathSource;
}
Expand All @@ -164,11 +167,6 @@ else if ( EntityDiscriminatorMapping.matchesRoleName( name ) ) {
}
}

@Override
public SqmSingularPersistentAttribute<? super J, ?> findIdAttribute() {
return super.findIdAttribute();
}

@Override
public SqmPathSource<?> getIdentifierDescriptor() {
return super.getIdentifierDescriptor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,11 @@ default <T> JpaSubQuery<T> except(Subquery<? extends T> query1, Subquery<?>... q
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Paths

<P, F> JpaExpression<F> fk(Path<P> path);
JpaExpression<?> id(Path<?> path);

JpaExpression<?> version(Path<?> path);

JpaExpression<?> fk(Path<?> path);

@Override
<X, T extends X> JpaPath<T> treat(Path<X> path, Class<T> type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,4 +324,7 @@ default <X> JpaJoin<?, X> join(JpaCteCriteria<X> cte, org.hibernate.query.common

@Override
<S extends T> JpaTreatedFrom<O,T,S> treatAs(EntityDomainType<S> treatJavaType);

@Incubating
JpaExpression<?> id();
}
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ public <N, T extends Temporal> JpaExpression<N> extract(TemporalField<N, T> fiel
}

@Override
public <P, F> JpaExpression<F> fk(Path<P> path) {
public JpaExpression<?> fk(Path<?> path) {
return criteriaBuilder.fk( path );
}

Expand Down Expand Up @@ -799,6 +799,16 @@ public JpaFunction<Instant> currentInstant() {
return criteriaBuilder.currentInstant();
}

@Override
public JpaExpression<?> id(Path<?> path) {
return criteriaBuilder.id( path );
}

@Override
public JpaExpression<?> version(Path<?> path) {
return criteriaBuilder.version( path );
}

@Override
public <T> JpaFunction<T> function(String name, Class<T> type, Expression<?>... args) {
return criteriaBuilder.function( name, type, args );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3472,7 +3472,8 @@ public SqmPath<?> visitEntityIdReference(HqlParser.EntityIdReferenceContext ctx)
return sqmPath.get( identifierDescriptor.getPathName(), true );
}
else if ( sqmPath instanceof SqmAnyValuedSimplePath<?> ) {
return sqmPath.resolvePathPart( AnyKeyPart.KEY_NAME, true, processingStateStack.getCurrent().getCreationState() );
return sqmPath.resolvePathPart( AnyKeyPart.KEY_NAME, true,
processingStateStack.getCurrent().getCreationState() );
}
else {
throw new FunctionArgumentException( "Argument '" + sqmPath.getNavigablePath()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,13 @@ <T> SqmJsonValueExpression<T> jsonValue(
SqmPredicate wrap(Expression<Boolean>... expressions);

@Override
<P, F> SqmExpression<F> fk(Path<P> path);
SqmExpression<?> fk(Path<?> path);

@Override
SqmExpression<?> id(Path<?> path);

@Override
SqmExpression<?> version(Path<?> path);

@Override
<X, T extends X> SqmPath<T> treat(Path<X> path, Class<T> type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.jpa.spi.JpaCompliance;
import org.hibernate.metamodel.mapping.EntityIdentifierMapping;
import org.hibernate.metamodel.mapping.EntityVersionMapping;
import org.hibernate.metamodel.model.domain.DomainType;
import org.hibernate.metamodel.model.domain.JpaMetamodel;
import org.hibernate.metamodel.model.domain.PersistentAttribute;
Expand Down Expand Up @@ -602,8 +604,8 @@ public <T extends HibernateCriteriaBuilder> T unwrap(Class<T> clazz) {
}

@Override
public <P, F> SqmExpression<F> fk(Path<P> path) {
final SqmPath<P> sqmPath = (SqmPath<P>) path;
public SqmPath<?> fk(Path<?> path) {
final SqmPath<?> sqmPath = (SqmPath<?>) path;
final SqmPathSource<?> toOneReference = sqmPath.getReferencedPathSource();
final boolean validToOneRef =
toOneReference.getBindableType() == Bindable.BindableType.SINGULAR_ATTRIBUTE
Expand All @@ -617,7 +619,6 @@ public <P, F> SqmExpression<F> fk(Path<P> path) {
)
);
}

return new SqmFkExpression<>( sqmPath );
}

Expand Down Expand Up @@ -2016,6 +2017,16 @@ public JpaExpression<LocalTime> localTime() {
);
}

@Override
public SqmPath<?> id(Path<?> path) {
return ((SqmPath<?>) path).get( EntityIdentifierMapping.ID_ROLE_NAME );
}

@Override
public SqmPath<?> version(Path<?> path) {
return ((SqmPath<?>) path).get( EntityVersionMapping.VERSION_ROLE_NAME );
}

@Override
public <T> SqmFunction<T> function(String name, Class<T> type, Expression<?>[] args) {
final BasicType<T> resultType = getTypeConfiguration().standardBasicTypeForJavaType( type );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.hibernate.query.criteria.JpaCrossJoin;
import org.hibernate.query.criteria.JpaCteCriteria;
import org.hibernate.query.criteria.JpaDerivedJoin;
import org.hibernate.query.criteria.JpaExpression;
import org.hibernate.query.criteria.JpaFunctionJoin;
import org.hibernate.query.criteria.JpaPath;
import org.hibernate.query.criteria.JpaSelection;
Expand Down Expand Up @@ -949,4 +950,9 @@ public JpaSelection<T> alias(String name) {
}
return super.alias( name );
}

@Override
public JpaExpression<?> id() {
return nodeBuilder().id( this );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.query.criteria;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Version;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

@SessionFactory
@DomainModel(annotatedClasses = CriteriaIdVersionTest.Thing.class)
class CriteriaIdVersionTest {
@Test
void test(SessionFactoryScope scope) {
scope.getSessionFactory().getSchemaManager().truncate();
scope.inTransaction( session -> {
session.persist( new Thing() );
} );
scope.inSession( session -> {
var cb = session.getCriteriaBuilder();
var cq = cb.createQuery( Long.class );
var root = cq.from( Thing.class );
cq.select( cb.id(root).as( Long.class ) );
assertEquals( 2L, session.createSelectionQuery( cq ).getSingleResult() );
} );
scope.inSession( session -> {
var cb = session.getCriteriaBuilder();
var cq = cb.createQuery( Long.class );
var root = cq.from( Thing.class );
cq.select( cb.version(root).as( Long.class ) );
assertEquals( 3L, session.createSelectionQuery( cq ).getSingleResult() );
} );
scope.inSession( session -> {
var cb = session.getCriteriaBuilder();
var cq = cb.createQuery( Long.class );
var root = cq.from( Thing.class );
cq.select( root.id().asLong() );
assertEquals( 2L, session.createSelectionQuery( cq ).getSingleResult() );
} );
}

@Test
void testPath(SessionFactoryScope scope) {
scope.getSessionFactory().getSchemaManager().truncate();
scope.inTransaction( session -> {
Thing thing = new Thing();
Thing otherThing = new Thing();
otherThing.id = 5;
thing.other = otherThing;
session.persist( thing );
} );
scope.inSession( session -> {
var cb = session.getCriteriaBuilder();
var cq = cb.createQuery( Long.class );
var root = cq.from( Thing.class );
cq.select( cb.id( root.get("other") ).as( Long.class ) );
cq.where( root.get("other").isNotNull() );
assertEquals( 5L, session.createSelectionQuery( cq ).getSingleResult() );
} );
}

@Entity
static class Thing {
@Id
long id = 2;
@Version
long version = 3;
@ManyToOne(cascade = CascadeType.PERSIST)
Thing other;
}
}