Skip to content

Commit 3b00822

Browse files
committed
Various code cleanup
1 parent 28560aa commit 3b00822

File tree

2 files changed

+112
-31
lines changed

2 files changed

+112
-31
lines changed

hibernate-core/src/test/java/org/hibernate/orm/test/jpa/query/NamedQueryTest.java

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,6 @@
44
*/
55
package org.hibernate.orm.test.jpa.query;
66

7-
import static org.assertj.core.api.Assertions.assertThat;
8-
import static org.hibernate.jpa.HibernateHints.HINT_NATIVE_LOCK_MODE;
9-
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
10-
import static org.junit.Assert.assertEquals;
11-
import static org.junit.Assert.assertThrows;
12-
13-
import java.util.List;
14-
15-
import org.hibernate.LockMode;
16-
import org.hibernate.Session;
17-
import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase;
18-
import org.hibernate.query.NativeQuery;
19-
20-
import org.hibernate.testing.orm.junit.JiraKey;
21-
import org.hibernate.testing.orm.junit.JiraKey;
22-
import org.junit.After;
23-
import org.junit.Before;
24-
import org.junit.Test;
25-
267
import jakarta.persistence.Entity;
278
import jakarta.persistence.GeneratedValue;
289
import jakarta.persistence.Id;
@@ -33,6 +14,20 @@
3314
import jakarta.persistence.Query;
3415
import jakarta.persistence.TypedQuery;
3516
import org.assertj.core.api.InstanceOfAssertFactories;
17+
import org.hibernate.Session;
18+
import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase;
19+
import org.hibernate.query.NativeQuery;
20+
import org.hibernate.testing.orm.junit.JiraKey;
21+
import org.junit.After;
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
25+
import java.util.List;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
29+
import static org.junit.Assert.assertEquals;
30+
import static org.junit.Assert.assertThrows;
3631

3732
/**
3833
* @author Andrea Boriero
@@ -270,18 +265,6 @@ public void testNamedQueryAddedFromEntityNativeQueryUsedAsUntyped() {
270265
} );
271266
}
272267

273-
@Test
274-
@JiraKey(value = "HHH-14816")
275-
public void testQueryHintLockMode() {
276-
doInJPA( this::entityManagerFactory, entityManager -> {
277-
Query query = entityManager.createNamedQuery( "NamedNativeQuery" );
278-
query.setHint( HINT_NATIVE_LOCK_MODE, "none" );
279-
query.setParameter( 1, GAME_TITLES[0] );
280-
assertEquals( LockMode.NONE, query.getHints().get( HINT_NATIVE_LOCK_MODE ) );
281-
}
282-
);
283-
}
284-
285268
@Entity(name = "Game")
286269
@NamedQueries(@NamedQuery(name = "NamedQuery", query = "select g from Game g where title = ?1"))
287270
@NamedNativeQueries(@NamedNativeQuery(name = "NamedNativeQuery", query = "select * from Game g where title = ?"))
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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

Comments
 (0)