-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
136 additions
and
0 deletions.
There are no files selected for viewing
136 changes: 136 additions & 0 deletions
136
hibernate-core/src/test/java/org/hibernate/orm/test/readonly/ReadOnlyNamedQueryTest.java
This file contains 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,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; | ||
} | ||
} | ||
} |