HHH-16132 - Dirty checking for collection-as-basic mappings
This commit is contained in:
parent
56223d3ebc
commit
85f98d21f9
|
@ -6,6 +6,7 @@
|
|||
*/
|
||||
package org.hibernate.userguide.mapping.basic;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.SortedSet;
|
||||
|
@ -16,7 +17,9 @@ import org.hibernate.metamodel.mapping.internal.BasicAttributeMapping;
|
|||
import org.hibernate.metamodel.spi.MappingMetamodelImplementor;
|
||||
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.Jira;
|
||||
import org.hibernate.testing.orm.junit.SessionFactory;
|
||||
import org.hibernate.testing.orm.junit.SessionFactoryScope;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
|
@ -25,6 +28,7 @@ import org.junit.jupiter.api.Test;
|
|||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.Table;
|
||||
import org.assertj.core.api.Assertions;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
@ -33,7 +37,7 @@ import static org.hamcrest.Matchers.equalTo;
|
|||
* Tests for mapping basic collections
|
||||
*/
|
||||
@DomainModel(annotatedClasses = BasicCollectionMappingTests.EntityOfCollections.class)
|
||||
@SessionFactory
|
||||
@SessionFactory( useCollectingStatementInspector = true )
|
||||
public class BasicCollectionMappingTests {
|
||||
|
||||
@Test
|
||||
|
@ -59,21 +63,76 @@ public class BasicCollectionMappingTests {
|
|||
final JdbcMapping jdbcMapping = attribute.getJdbcMapping();
|
||||
assertThat(jdbcMapping.getJavaTypeDescriptor().getJavaTypeClass(), equalTo(SortedSet.class));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// and try to use the mapping
|
||||
scope.inTransaction(
|
||||
(session) -> session.persist(
|
||||
new EntityOfCollections(
|
||||
@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 ) )
|
||||
)
|
||||
)
|
||||
);
|
||||
scope.inTransaction(
|
||||
(session) -> session.get( EntityOfCollections.class, 1)
|
||||
session.persist( entity );
|
||||
} );
|
||||
|
||||
final SQLStatementInspector statementInspector = scope.getCollectingStatementInspector();
|
||||
|
||||
scope.inTransaction( (session) -> {
|
||||
final EntityOfCollections entity = session.get( EntityOfCollections.class, 1 );
|
||||
statementInspector.clear();
|
||||
entity.list = List.of( (short) 3 );
|
||||
entity.sortedSet = new TreeSet<>( Set.of( (short) 5 ) );
|
||||
} );
|
||||
Assertions.assertThat( statementInspector.getSqlQueries() ).isEmpty();
|
||||
|
||||
scope.inTransaction( (session) -> {
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue