|
| 1 | +package org.hibernate.test.pagination; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import javax.persistence.Entity; |
| 5 | +import javax.persistence.GeneratedValue; |
| 6 | +import javax.persistence.Id; |
| 7 | +import javax.persistence.criteria.CriteriaQuery; |
| 8 | +import javax.persistence.criteria.Root; |
| 9 | + |
| 10 | +import org.hibernate.LockMode; |
| 11 | +import org.hibernate.LockOptions; |
| 12 | +import org.hibernate.cfg.Configuration; |
| 13 | +import org.hibernate.cfg.Environment; |
| 14 | +import org.hibernate.dialect.Oracle12cDialect; |
| 15 | +import org.hibernate.resource.jdbc.spi.StatementInspector; |
| 16 | + |
| 17 | +import org.hibernate.testing.RequiresDialect; |
| 18 | +import org.hibernate.testing.TestForIssue; |
| 19 | +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; |
| 20 | +import org.junit.After; |
| 21 | +import org.junit.Before; |
| 22 | +import org.junit.Test; |
| 23 | + |
| 24 | +import static org.junit.Assert.assertEquals; |
| 25 | +import static org.junit.Assert.assertFalse; |
| 26 | +import static org.junit.Assert.assertTrue; |
| 27 | + |
| 28 | +@RequiresDialect(Oracle12cDialect.class) |
| 29 | +@TestForIssue(jiraKey = "HHH-14624") |
| 30 | +public class OraclePaginationWithLocksTest extends BaseCoreFunctionalTestCase { |
| 31 | +private static final MostRecentStatementInspector mostRecentStatementInspector = new MostRecentStatementInspector(); |
| 32 | + |
| 33 | +@Override |
| 34 | +protected Class<?>[] getAnnotatedClasses() { |
| 35 | +return new Class[] { Person.class }; |
| 36 | +} |
| 37 | + |
| 38 | +@Override |
| 39 | +protected void configure(Configuration configuration) { |
| 40 | +super.configure( configuration ); |
| 41 | +configuration.getProperties().put( Environment.STATEMENT_INSPECTOR, mostRecentStatementInspector ); |
| 42 | +} |
| 43 | + |
| 44 | +@Before |
| 45 | +public void setUp() { |
| 46 | +inTransaction( |
| 47 | +session -> { |
| 48 | +for ( int i = 0; i < 20; i++ ) { |
| 49 | +session.persist( new Person( "name" + i ) ); |
| 50 | +} |
| 51 | +session.persist( new Person( "for update" ) ); |
| 52 | +} |
| 53 | +); |
| 54 | +} |
| 55 | + |
| 56 | +@After |
| 57 | +public void tearDown() { |
| 58 | +inTransaction( |
| 59 | +session -> |
| 60 | +session.createQuery( "delete from Person" ).executeUpdate() |
| 61 | + |
| 62 | +); |
| 63 | +} |
| 64 | + |
| 65 | +@Test |
| 66 | +public void testNativeQuery() { |
| 67 | +inTransaction( |
| 68 | +session -> { |
| 69 | +final List<Person> people = session.createNativeQuery( "select * from Person for update" ) |
| 70 | +.setMaxResults( 10 ) |
| 71 | +.list(); |
| 72 | +assertEquals( 10, people.size() ); |
| 73 | +assertFalse( mostRecentStatementInspector.sqlContains( "fetch" ) ); |
| 74 | +} |
| 75 | +); |
| 76 | + |
| 77 | +inTransaction( |
| 78 | +session -> { |
| 79 | +final List<Person> people = session.createNativeQuery( "select * from Person" ) |
| 80 | +.setMaxResults( 10 ) |
| 81 | +.list(); |
| 82 | +assertEquals( 10, people.size() ); |
| 83 | +assertTrue( mostRecentStatementInspector.sqlContains( "fetch" ) ); |
| 84 | +} |
| 85 | +); |
| 86 | + |
| 87 | +inTransaction( |
| 88 | +session -> { |
| 89 | +final List<Person> people = session.createNativeQuery( "select * from Person" ) |
| 90 | +.setFirstResult( 3 ) |
| 91 | +.setMaxResults( 10 ) |
| 92 | +.list(); |
| 93 | +assertEquals( 10, people.size() ); |
| 94 | +assertTrue( mostRecentStatementInspector.sqlContains( "fetch" ) ); |
| 95 | +} |
| 96 | +); |
| 97 | +} |
| 98 | + |
| 99 | +@Test |
| 100 | +public void testCriteriaQuery() { |
| 101 | +inTransaction( |
| 102 | +session -> { |
| 103 | +final CriteriaQuery<Person> query = session.getCriteriaBuilder().createQuery( Person.class ); |
| 104 | +final Root<Person> root = query.from( Person.class ); |
| 105 | +query.select( root ); |
| 106 | +final List<Person> people = session.createQuery( query ) |
| 107 | +.setMaxResults( 10 ) |
| 108 | +.setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE ).setFollowOnLocking( false ) ) |
| 109 | +.getResultList(); |
| 110 | +assertEquals( 10, people.size() ); |
| 111 | +assertFalse( mostRecentStatementInspector.sqlContains( "fetch" ) ); |
| 112 | +} |
| 113 | +); |
| 114 | + |
| 115 | +inTransaction( |
| 116 | +session -> { |
| 117 | +final CriteriaQuery<Person> query = session.getCriteriaBuilder().createQuery( Person.class ); |
| 118 | +final Root<Person> root = query.from( Person.class ); |
| 119 | +query.select( root ); |
| 120 | +final List<Person> people = session.createQuery( query ) |
| 121 | +.setMaxResults( 10 ) |
| 122 | +.getResultList(); |
| 123 | +assertEquals( 10, people.size() ); |
| 124 | +assertTrue( mostRecentStatementInspector.sqlContains( "fetch" ) ); |
| 125 | +} |
| 126 | +); |
| 127 | + |
| 128 | +inTransaction( |
| 129 | +session -> { |
| 130 | +final CriteriaQuery<Person> query = session.getCriteriaBuilder().createQuery( Person.class ); |
| 131 | +final Root<Person> root = query.from( Person.class ); |
| 132 | +query.select( root ); |
| 133 | +final List<Person> people = session.createQuery( query ) |
| 134 | +.setMaxResults( 10 ) |
| 135 | +.setFirstResult( 2 ) |
| 136 | +.getResultList(); |
| 137 | +assertEquals( 10, people.size() ); |
| 138 | +assertTrue( mostRecentStatementInspector.sqlContains( "fetch" ) ); |
| 139 | +} |
| 140 | +); |
| 141 | + |
| 142 | +} |
| 143 | + |
| 144 | +@Test |
| 145 | +public void testHqlQuery() { |
| 146 | +inTransaction( |
| 147 | +session -> { |
| 148 | +List<Person> people = session.createQuery( |
| 149 | +"select p from Person p", Person.class ) |
| 150 | +.setMaxResults( 10 ) |
| 151 | +.setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE ).setFollowOnLocking( false ) ) |
| 152 | +.getResultList(); |
| 153 | +assertEquals( 10, people.size() ); |
| 154 | +assertFalse( mostRecentStatementInspector.sqlContains( "fetch" ) ); |
| 155 | +} |
| 156 | +); |
| 157 | + |
| 158 | +inTransaction( |
| 159 | +session -> { |
| 160 | +List<Person> people = session.createQuery( |
| 161 | +"select p from Person p", Person.class ) |
| 162 | +.setMaxResults( 10 ) |
| 163 | +.getResultList(); |
| 164 | +assertEquals( 10, people.size() ); |
| 165 | +assertTrue( mostRecentStatementInspector.sqlContains( "fetch" ) ); |
| 166 | +} |
| 167 | +); |
| 168 | + |
| 169 | +inTransaction( |
| 170 | +session -> { |
| 171 | +List<Person> people = session.createQuery( |
| 172 | +"select p from Person p", Person.class ) |
| 173 | +.setFirstResult( 2 ) |
| 174 | +.setMaxResults( 10 ) |
| 175 | +.getResultList(); |
| 176 | +assertEquals( 10, people.size() ); |
| 177 | +assertEquals( 10, people.size() ); |
| 178 | +assertTrue( mostRecentStatementInspector.sqlContains( "fetch" ) ); |
| 179 | +} |
| 180 | +); |
| 181 | + |
| 182 | +inTransaction( |
| 183 | +session -> { |
| 184 | +List<Person> people = session.createQuery( |
| 185 | +"select p from Person p where p.name = 'for update'", Person.class ) |
| 186 | +.setMaxResults( 10 ) |
| 187 | +.setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE ).setFollowOnLocking( false ) ) |
| 188 | +.getResultList(); |
| 189 | +assertEquals( 1, people.size() ); |
| 190 | +assertFalse( mostRecentStatementInspector.sqlContains( "fetch" ) ); |
| 191 | +} |
| 192 | +); |
| 193 | + |
| 194 | +inTransaction( |
| 195 | +session -> { |
| 196 | +List<Person> people = session.createQuery( |
| 197 | +"select p from Person p where p.name = 'for update'", Person.class ) |
| 198 | +.setMaxResults( 10 ) |
| 199 | +.getResultList(); |
| 200 | +assertEquals( 1, people.size() ); |
| 201 | +assertTrue( mostRecentStatementInspector.sqlContains( "fetch" ) ); |
| 202 | +} |
| 203 | +); |
| 204 | + |
| 205 | + |
| 206 | +} |
| 207 | + |
| 208 | +private static class MostRecentStatementInspector implements StatementInspector { |
| 209 | +private String mostRecentSql; |
| 210 | + |
| 211 | +public String inspect(String sql) { |
| 212 | +mostRecentSql = sql; |
| 213 | +return sql; |
| 214 | +} |
| 215 | + |
| 216 | +public boolean sqlContains(String toCheck) { |
| 217 | +return mostRecentSql.contains( toCheck ); |
| 218 | +} |
| 219 | + |
| 220 | +} |
| 221 | + |
| 222 | +@Entity(name = "Person") |
| 223 | +public static class Person { |
| 224 | +@Id |
| 225 | +@GeneratedValue |
| 226 | +private Long id; |
| 227 | + |
| 228 | +private String name; |
| 229 | + |
| 230 | +public Person() { |
| 231 | +} |
| 232 | + |
| 233 | +public Person(String name) { |
| 234 | +this.name = name; |
| 235 | +} |
| 236 | + |
| 237 | +public Long getId() { |
| 238 | +return id; |
| 239 | +} |
| 240 | + |
| 241 | +public void setId(Long id) { |
| 242 | +this.id = id; |
| 243 | +} |
| 244 | + |
| 245 | +public String getName() { |
| 246 | +return name; |
| 247 | +} |
| 248 | + |
| 249 | +public void setName(String name) { |
| 250 | +this.name = name; |
| 251 | +} |
| 252 | +} |
| 253 | +} |
0 commit comments