|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.jpa.query; |
| 6 | + |
| 7 | +import jakarta.persistence.LockModeType; |
| 8 | +import jakarta.persistence.Query; |
| 9 | +import org.hibernate.LockMode; |
| 10 | +import org.hibernate.dialect.H2Dialect; |
| 11 | +import org.hibernate.jpa.HibernateHints; |
| 12 | +import org.hibernate.query.sql.spi.NativeQueryImplementor; |
| 13 | +import org.hibernate.testing.jdbc.SQLStatementInspector; |
| 14 | +import org.hibernate.testing.orm.domain.gambit.SimpleEntity; |
| 15 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 16 | +import org.hibernate.testing.orm.junit.RequiresDialect; |
| 17 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 18 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | + |
| 22 | +import java.util.Locale; |
| 23 | + |
| 24 | +import static org.assertj.core.api.Assertions.assertThat; |
| 25 | +import static org.junit.jupiter.api.Assertions.fail; |
| 26 | + |
| 27 | +/** |
| 28 | + * @author Steve Ebersole |
| 29 | + */ |
| 30 | +@SuppressWarnings("JUnitMalformedDeclaration") |
| 31 | +@DomainModel(annotatedClasses = SimpleEntity.class) |
| 32 | +@SessionFactory(useCollectingStatementInspector = true) |
| 33 | +public class NativeQueryLockingTests { |
| 34 | + final String QUERY_STRING = "select * from SIMPLE_ENTITY"; |
| 35 | + |
| 36 | + @Test |
| 37 | + void testJpaLockMode(SessionFactoryScope sessions) { |
| 38 | + // JPA says this is illegal |
| 39 | + |
| 40 | + sessions.inTransaction( (session) -> { |
| 41 | + final Query query = session.createNativeQuery( QUERY_STRING, SimpleEntity.class ); |
| 42 | + try { |
| 43 | + query.setLockMode( LockModeType.PESSIMISTIC_WRITE ); |
| 44 | + fail( "Expecting failure per JPA" ); |
| 45 | + } |
| 46 | + catch (IllegalStateException e) { |
| 47 | + assertThat( e ).hasMessageContaining( "lock mode" ); |
| 48 | + } |
| 49 | + } ); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + @RequiresDialect( value = H2Dialect.class, comment = "This has more to do with Query internals than the DB; so avoid Dialect variances in generated SQL" ) |
| 54 | + void testHibernateLockMode(SessionFactoryScope sessions) { |
| 55 | + final SQLStatementInspector sqlCollector = sessions.getCollectingStatementInspector(); |
| 56 | + sqlCollector.clear(); |
| 57 | + |
| 58 | + sessions.inTransaction( (session) -> { |
| 59 | + final NativeQueryImplementor<SimpleEntity> query = session.createNativeQuery( QUERY_STRING, SimpleEntity.class ); |
| 60 | + query.setHibernateLockMode( LockMode.PESSIMISTIC_WRITE ); |
| 61 | + query.list(); |
| 62 | + |
| 63 | + assertThat( sqlCollector.getSqlQueries() ).hasSize( 1 ); |
| 64 | + assertThat( sqlCollector.getSqlQueries().get( 0 ) ).endsWith( " for update" ); |
| 65 | + } ); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + @RequiresDialect( value = H2Dialect.class, comment = "This has more to do with Query internals than the DB; so avoid Dialect variances in generated SQL" ) |
| 70 | + void testLockModeHint(SessionFactoryScope sessions) { |
| 71 | + final SQLStatementInspector sqlCollector = sessions.getCollectingStatementInspector(); |
| 72 | + sqlCollector.clear(); |
| 73 | + |
| 74 | + sessions.inTransaction( (session) -> { |
| 75 | + final Query query = session.createNativeQuery( QUERY_STRING, SimpleEntity.class ); |
| 76 | + query.setHint( HibernateHints.HINT_NATIVE_LOCK_MODE, LockMode.PESSIMISTIC_WRITE ); |
| 77 | + query.getResultList(); |
| 78 | + |
| 79 | + assertThat( sqlCollector.getSqlQueries() ).hasSize( 1 ); |
| 80 | + assertThat( sqlCollector.getSqlQueries().get( 0 ) ).endsWith( " for update" ); |
| 81 | + } ); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void testLockModeHintLowercase(SessionFactoryScope sessions) { |
| 86 | + final SQLStatementInspector sqlCollector = sessions.getCollectingStatementInspector(); |
| 87 | + sqlCollector.clear(); |
| 88 | + |
| 89 | + sessions.inTransaction( (session) -> { |
| 90 | + final Query query = session.createNativeQuery( QUERY_STRING, SimpleEntity.class ); |
| 91 | + query.setHint( HibernateHints.HINT_NATIVE_LOCK_MODE, LockMode.PESSIMISTIC_WRITE.name().toLowerCase( Locale.ROOT ) ); |
| 92 | + query.getResultList(); |
| 93 | + |
| 94 | + assertThat( sqlCollector.getSqlQueries() ).hasSize( 1 ); |
| 95 | + assertThat( sqlCollector.getSqlQueries().get( 0 ) ).endsWith( " for update" ); |
| 96 | + } ); |
| 97 | + } |
| 98 | +} |
0 commit comments