-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
HHH-19781 Subsequent uses of Criteria SelectionSpecification lead to duplicated specifications #11009
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
HHH-19781 Subsequent uses of Criteria SelectionSpecification lead to duplicated specifications #11009
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
175 changes: 175 additions & 0 deletions
175
...rnate-core/src/test/java/org/hibernate/orm/test/query/dynamic/SpecificationReuseTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.orm.test.query.dynamic; | ||
|
||
import java.util.function.Consumer; | ||
|
||
import org.hibernate.query.Order; | ||
import org.hibernate.query.restriction.Restriction; | ||
import org.hibernate.query.specification.MutationSpecification; | ||
import org.hibernate.query.specification.SelectionSpecification; | ||
|
||
import org.hibernate.testing.orm.junit.DomainModel; | ||
import org.hibernate.testing.orm.junit.Jira; | ||
import org.hibernate.testing.orm.junit.SessionFactory; | ||
import org.hibernate.testing.orm.junit.SessionFactoryScope; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@DomainModel(annotatedClasses = { | ||
BasicEntity.class, | ||
OtherEntity.class, | ||
}) | ||
@SessionFactory(useCollectingStatementInspector = true) | ||
@Jira( "https://hibernate.atlassian.net/browse/HHH-19781" ) | ||
public class SpecificationReuseTest { | ||
@Test | ||
public void dynamicSelect(SessionFactoryScope scope) { | ||
final var inspector = scope.getCollectingStatementInspector(); | ||
scope.inTransaction( session -> { | ||
final var spec = SelectionSpecification.create( BasicEntity.class ) | ||
.sort( Order.asc( BasicEntity_.position ) ) | ||
.restrict( Restriction.like( BasicEntity_.name, "entity_%" ) ); | ||
nTimes( spec, 3, s -> { | ||
inspector.clear(); | ||
assertThat( s.createQuery( session ).list() ).extracting( BasicEntity::getId ).containsExactly( 2, 1 ); | ||
inspector.assertNumberOfOccurrenceInQueryNoSpace( 0, "position", 2 ); | ||
inspector.assertNumberOfOccurrenceInQueryNoSpace( 0, "name", 2 ); | ||
} ); | ||
} ); | ||
} | ||
|
||
@Test | ||
public void hqlSelect(SessionFactoryScope scope) { | ||
final var inspector = scope.getCollectingStatementInspector(); | ||
scope.inTransaction( session -> { | ||
final var spec = SelectionSpecification.create( BasicEntity.class, "from BasicEntity" ) | ||
.sort( Order.asc( BasicEntity_.position ) ) | ||
.restrict( Restriction.like( BasicEntity_.name, "entity_%" ) ); | ||
nTimes( spec, 3, s -> { | ||
inspector.clear(); | ||
assertThat( s.createQuery( session ).list() ).extracting( BasicEntity::getId ).containsExactly( 2, 1 ); | ||
inspector.assertNumberOfOccurrenceInQueryNoSpace( 0, "position", 2 ); | ||
inspector.assertNumberOfOccurrenceInQueryNoSpace( 0, "name", 2 ); | ||
} ); | ||
} ); | ||
} | ||
|
||
@Test | ||
public void criteriaSelect(SessionFactoryScope scope) { | ||
final var inspector = scope.getCollectingStatementInspector(); | ||
scope.inTransaction( session -> { | ||
final var cb = session.getCriteriaBuilder(); | ||
final var query = cb.createQuery( BasicEntity.class ); | ||
final var root = query.from( BasicEntity.class ); | ||
final var spec = SelectionSpecification.create( query.select( root ) ) | ||
.sort( Order.asc( BasicEntity_.position ) ) | ||
.restrict( Restriction.like( BasicEntity_.name, "entity_%" ) ); | ||
nTimes( spec, 3, s -> { | ||
inspector.clear(); | ||
assertThat( s.createQuery( session ).list() ).extracting( BasicEntity::getId ).containsExactly( 2, 1 ); | ||
inspector.assertNumberOfOccurrenceInQueryNoSpace( 0, "position", 2 ); | ||
inspector.assertNumberOfOccurrenceInQueryNoSpace( 0, "name", 2 ); | ||
} ); | ||
} ); | ||
} | ||
|
||
@Test | ||
public void hqlMutation(SessionFactoryScope scope) { | ||
final var inspector = scope.getCollectingStatementInspector(); | ||
scope.inTransaction( session -> { | ||
final var e = new BasicEntity(); | ||
e.setId( 33 ); | ||
session.persist( e ); | ||
} ); | ||
scope.inTransaction( session -> { | ||
final var spec = MutationSpecification.create( BasicEntity.class, "update BasicEntity set name = 'entity_33'" ) | ||
.restrict( Restriction.equal( BasicEntity_.id, 33 ) ); | ||
nTimes( spec, 3, s -> { | ||
inspector.clear(); | ||
assertThat( s.createQuery( session ).executeUpdate() ).isEqualTo( 1 ); | ||
inspector.assertNumberOfOccurrenceInQueryNoSpace( 0, "id", 1 ); | ||
} ); | ||
} ); | ||
scope.inTransaction( session -> { | ||
final var spec = MutationSpecification.create( BasicEntity.class, "delete BasicEntity" ) | ||
.restrict( Restriction.equal( BasicEntity_.name, "entity_33" ) ); | ||
nTimes( spec, 3, s -> { | ||
inspector.clear(); | ||
s.createQuery( session ).executeUpdate(); | ||
inspector.assertNumberOfOccurrenceInQueryNoSpace( 0, "name", 1 ); | ||
} ); | ||
} ); | ||
} | ||
|
||
@Test | ||
public void criteriaMutation(SessionFactoryScope scope) { | ||
final var inspector = scope.getCollectingStatementInspector(); | ||
scope.inTransaction( session -> { | ||
final var e = new BasicEntity(); | ||
e.setId( 44 ); | ||
session.persist( e ); | ||
} ); | ||
scope.inTransaction( session -> { | ||
final var cb = session.getCriteriaBuilder(); | ||
final var cu = cb.createCriteriaUpdate( BasicEntity.class ); | ||
final var spec = MutationSpecification.create( cu.set( BasicEntity_.name, "entity_44" ) ) | ||
.restrict( Restriction.equal( BasicEntity_.id, 44 ) ); | ||
nTimes( spec, 3, s -> { | ||
inspector.clear(); | ||
assertThat( s.createQuery( session ).executeUpdate() ).isEqualTo( 1 ); | ||
inspector.assertNumberOfOccurrenceInQueryNoSpace( 0, "id", 1 ); | ||
} ); | ||
} ); | ||
scope.inTransaction( session -> { | ||
final var cb = session.getCriteriaBuilder(); | ||
final var cd = cb.createCriteriaDelete( BasicEntity.class ); | ||
final var spec = MutationSpecification.create( cd ) | ||
.restrict( Restriction.equal( BasicEntity_.name, "entity_44" ) ); | ||
nTimes( spec, 3, s -> { | ||
inspector.clear(); | ||
s.createQuery( session ).executeUpdate(); | ||
inspector.assertNumberOfOccurrenceInQueryNoSpace( 0, "name", 1 ); | ||
} ); | ||
} ); | ||
} | ||
|
||
<T> void nTimes(SelectionSpecification<T> spec, int n, Consumer<SelectionSpecification<T>> consumer) { | ||
for ( int i = 0; i < n; i++ ) { | ||
consumer.accept( spec ); | ||
} | ||
} | ||
|
||
<T> void nTimes(MutationSpecification<T> spec, int n, Consumer<MutationSpecification<T>> consumer) { | ||
for ( int i = 0; i < n; i++ ) { | ||
consumer.accept( spec ); | ||
} | ||
} | ||
|
||
@BeforeAll | ||
public void setUp(SessionFactoryScope scope) { | ||
scope.inTransaction( session -> { | ||
final var e1 = new BasicEntity(); | ||
e1.setId( 1 ); | ||
e1.setName( "entity_1" ); | ||
e1.setPosition( 99 ); | ||
session.persist( e1 ); | ||
|
||
final var e2 = new BasicEntity(); | ||
e2.setId( 2 ); | ||
e2.setName( "entity_2" ); | ||
e2.setPosition( 42 ); | ||
session.persist( e2 ); | ||
} ); | ||
} | ||
|
||
@AfterAll | ||
public void tearDown(SessionFactoryScope scope) { | ||
scope.getSessionFactory().getSchemaManager().truncateMappedObjects(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.