test for @EntityResult(lockMode)

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-05-13 00:02:30 +02:00 committed by Steve Ebersole
parent 27042f562f
commit e11ac26cd2
2 changed files with 32 additions and 0 deletions

View File

@ -17,6 +17,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
import jakarta.persistence.LockModeType;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.query.named.NamedResultSetMappingMemento;
@ -119,6 +120,29 @@ public class EntityResultTests extends AbstractUsageTest {
);
}
@Test
public void testImplicitAttributeMappingWithLockMode(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
// make sure it is in the repository
final NamedResultSetMappingMemento mappingMemento = session.getSessionFactory()
.getQueryEngine()
.getNamedObjectRepository()
.getResultSetMappingMemento(
"entity-lockmode" );
assertThat( mappingMemento, notNullValue() );
// apply it to a native-query
final String qryString = "select id, name, notes from SimpleEntityWithNamedMappings for update";
final List<SimpleEntityWithNamedMappings> results = session
.createNativeQuery( qryString, "entity-lockmode" )
.list();
assertThat( results.size(), is( 1 ) );
assertThat( session.getLockMode(results.get(0)), is(LockModeType.PESSIMISTIC_WRITE));
}
);
}
@Test
public void testMixedAttributeMapping(SessionFactoryScope scope) {
scope.inTransaction(

View File

@ -14,6 +14,7 @@ import jakarta.persistence.EntityResult;
import jakarta.persistence.FetchType;
import jakarta.persistence.FieldResult;
import jakarta.persistence.Id;
import jakarta.persistence.LockModeType;
import jakarta.persistence.SqlResultSetMapping;
/**
@ -57,6 +58,13 @@ import jakarta.persistence.SqlResultSetMapping;
entityClass = SimpleEntityWithNamedMappings.class
)
)
@SqlResultSetMapping(
name = "entity-lockmode",
entities = @EntityResult(
entityClass = SimpleEntityWithNamedMappings.class,
lockMode = LockModeType.PESSIMISTIC_WRITE
)
)
@SqlResultSetMapping(
name = "entity-id-notes",
entities = @EntityResult(