HHH-17313 Add test for issue

This commit is contained in:
Marco Belladelli 2023-11-13 14:23:02 +01:00
parent b697f3573d
commit 1486d5693b
1 changed files with 136 additions and 0 deletions

View File

@ -0,0 +1,136 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.orm.test.readonly;
import java.util.function.Consumer;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.Jira;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.NamedQuery;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hibernate.jpa.HibernateHints.HINT_READ_ONLY;
/**
* @author Marco Belladelli
*/
@DomainModel( annotatedClasses = ReadOnlyNamedQueryTest.TestEntity.class )
@Jira( "https://hibernate.atlassian.net/browse/HHH-17313" )
public class ReadOnlyNamedQueryTest extends AbstractReadOnlyTest {
@AfterEach
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction( session -> session.createMutationQuery( "delete from TestEntity" ).executeUpdate() );
}
@Test
public void testSetReadOnly(SessionFactoryScope scope) {
executeTest( scope, session -> {
final TestEntity entity = session.createNamedQuery(
"TestEntity.selectAll",
TestEntity.class
).getSingleResult();
assertThat( entity.getData() ).isEqualTo( "original" );
session.setReadOnly( entity, true );
assertThat( session.isReadOnly( entity ) ).isTrue();
entity.setData( "changed" );
assertThat( entity.getData() ).isEqualTo( "changed" );
} );
}
@Test
public void testReadOnlyHint(SessionFactoryScope scope) {
executeTest( scope, session -> {
session.setDefaultReadOnly( true );
final TestEntity entity = session.createNamedQuery(
"TestEntity.selectAll",
TestEntity.class
).setHint( HINT_READ_ONLY, true ).getSingleResult();
assertThat( entity.getData() ).isEqualTo( "original" );
assertThat( session.isReadOnly( entity ) ).isTrue();
entity.setData( "changed" );
assertThat( entity.getData() ).isEqualTo( "changed" );
} );
}
@Test
public void testDefaultReadOnly(SessionFactoryScope scope) {
executeTest( scope, session -> {
session.setDefaultReadOnly( true );
final TestEntity entity = session.createNamedQuery(
"TestEntity.selectAll",
TestEntity.class
).getSingleResult();
assertThat( entity.getData() ).isEqualTo( "original" );
assertThat( session.isReadOnly( entity ) ).isTrue();
entity.setData( "changed" );
assertThat( entity.getData() ).isEqualTo( "changed" );
} );
}
private void executeTest(SessionFactoryScope scope, Consumer<SessionImplementor> namedQueryExecutor) {
clearCounts( scope );
scope.inTransaction( session -> {
final TestEntity entity = new TestEntity( 1, "original" );
session.persist( entity );
} );
assertInsertCount( 1, scope );
assertUpdateCount( 0, scope );
clearCounts( scope );
scope.inTransaction( namedQueryExecutor );
assertUpdateCount( 0, scope );
assertUpdateCount( 0, scope );
scope.inTransaction( session -> {
final TestEntity entity = session.find( TestEntity.class, 1 );
assertThat( entity.getData() ).isEqualTo( "original" );
} );
}
@Entity( name = "TestEntity" )
@NamedQuery( name = "TestEntity.selectAll", query = "select t from TestEntity t" )
public static class TestEntity {
@Id
private Integer id;
private String data;
public TestEntity() {
}
public TestEntity(Integer id, String data) {
this.id = id;
this.data = data;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
}