HHH-16132 - Dirty checking for collection-as-basic mappings

This commit is contained in:
Steve Ebersole 2023-03-16 09:07:54 -05:00
parent 60d2623abe
commit bab4be3d14

View File

@ -6,6 +6,7 @@
*/ */
package org.hibernate.userguide.mapping.basic; package org.hibernate.userguide.mapping.basic;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.SortedSet; import java.util.SortedSet;
@ -16,7 +17,9 @@
import org.hibernate.metamodel.spi.MappingMetamodelImplementor; import org.hibernate.metamodel.spi.MappingMetamodelImplementor;
import org.hibernate.persister.entity.EntityPersister; import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.testing.jdbc.SQLStatementInspector;
import org.hibernate.testing.orm.junit.DomainModel; 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.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope; import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
@ -25,6 +28,7 @@
import jakarta.persistence.Entity; import jakarta.persistence.Entity;
import jakarta.persistence.Id; import jakarta.persistence.Id;
import jakarta.persistence.Table; import jakarta.persistence.Table;
import org.assertj.core.api.Assertions;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
@ -33,7 +37,7 @@
* Tests for mapping basic collections * Tests for mapping basic collections
*/ */
@DomainModel(annotatedClasses = BasicCollectionMappingTests.EntityOfCollections.class) @DomainModel(annotatedClasses = BasicCollectionMappingTests.EntityOfCollections.class)
@SessionFactory @SessionFactory( useCollectingStatementInspector = true )
public class BasicCollectionMappingTests { public class BasicCollectionMappingTests {
@Test @Test
@ -59,21 +63,76 @@ public void testMappings(SessionFactoryScope scope) {
final JdbcMapping jdbcMapping = attribute.getJdbcMapping(); final JdbcMapping jdbcMapping = attribute.getJdbcMapping();
assertThat(jdbcMapping.getJavaTypeDescriptor().getJavaTypeClass(), equalTo(SortedSet.class)); assertThat(jdbcMapping.getJavaTypeDescriptor().getJavaTypeClass(), equalTo(SortedSet.class));
} }
}
@Test
@Jira( "https://hibernate.atlassian.net/browse/HHH-16132" )
public void testDirtyCheckingManaged(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
final EntityOfCollections entity = new EntityOfCollections(
1,
List.of( (short) 3 ),
new TreeSet<>( Set.of( (short) 5 ) )
);
session.persist( entity );
} );
// and try to use the mapping final SQLStatementInspector statementInspector = scope.getCollectingStatementInspector();
scope.inTransaction(
(session) -> session.persist( scope.inTransaction( (session) -> {
new EntityOfCollections( final EntityOfCollections entity = session.get( EntityOfCollections.class, 1 );
1, statementInspector.clear();
List.of( (short) 3 ), entity.list = List.of( (short) 3 );
new TreeSet<>( Set.of( (short) 5 ) ) entity.sortedSet = new TreeSet<>( Set.of( (short) 5 ) );
) } );
) Assertions.assertThat( statementInspector.getSqlQueries() ).isEmpty();
);
scope.inTransaction( scope.inTransaction( (session) -> {
(session) -> session.get( EntityOfCollections.class, 1) final EntityOfCollections entity = session.get( EntityOfCollections.class, 1 );
); statementInspector.clear();
entity.list = List.of( (short) 4 );
} );
Assertions.assertThat( statementInspector.getSqlQueries() ).hasSize( 1 );
Assertions.assertThat( statementInspector.getSqlQueries().get( 0 ) ).startsWith( "update " );
scope.inTransaction( (session) -> {
final EntityOfCollections entity = session.get( EntityOfCollections.class, 1 );
statementInspector.clear();
entity.list.add( (short) 55 );
} );
Assertions.assertThat( statementInspector.getSqlQueries() ).hasSize( 1 );
Assertions.assertThat( statementInspector.getSqlQueries().get( 0 ) ).startsWith( "update " );
}
@Test
@Jira( "https://hibernate.atlassian.net/browse/HHH-16132" )
public void testDirtyCheckingDetached(SessionFactoryScope scope) {
final SQLStatementInspector statementInspector = scope.getCollectingStatementInspector();
final EntityOfCollections created = scope.fromTransaction( (session) -> {
final EntityOfCollections entity = new EntityOfCollections(
1,
List.of( (short) 3 ),
new TreeSet<>( Set.of( (short) 5 ) )
);
session.persist( entity );
return entity;
} );
created.list = new ArrayList<>( List.of( (short) 3 ) );
created.sortedSet = new TreeSet<>( Set.of( (short) 5 ) );
statementInspector.clear();
final EntityOfCollections merged = scope.fromTransaction( (session) -> session.merge( created ) );
Assertions.assertThat( statementInspector.getSqlQueries() ).hasSize( 1 );
Assertions.assertThat( statementInspector.getSqlQueries().get( 0 ) ).startsWith( "select " );
merged.list.add( (short) 55 );
statementInspector.clear();
final EntityOfCollections merged2 = scope.fromTransaction( (session) -> session.merge( merged ) );
Assertions.assertThat( statementInspector.getSqlQueries() ).hasSize( 2 );
Assertions.assertThat( statementInspector.getSqlQueries().get( 0 ) ).startsWith( "select " );
Assertions.assertThat( statementInspector.getSqlQueries().get( 1 ) ).startsWith( "update " );
} }
@AfterEach @AfterEach