Skip to content

Commit 2952b60

Browse files
dreab8Sanne
authored andcommitted
HHH-14624 add test
1 parent fa26119 commit 2952b60

File tree

2 files changed

+312
-34
lines changed

2 files changed

+312
-34
lines changed

hibernate-core/src/test/java/org/hibernate/test/pagination/OraclePaginationTest.java

Lines changed: 59 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
import org.hibernate.testing.RequiresDialect;
1818
import org.hibernate.testing.TestForIssue;
19+
import org.junit.After;
20+
import org.junit.Before;
1921
import org.junit.Test;
2022

2123
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
@@ -34,39 +36,8 @@ protected Class<?>[] getAnnotatedClasses() {
3436
};
3537
}
3638

37-
@Entity(name = "RootEntity")
38-
@Table(name = "V_MYTABLE_LAST")
39-
public static class RootEntity implements Serializable {
40-
41-
@Id
42-
private Long id;
43-
44-
@Id
45-
private Long version;
46-
47-
private String caption;
48-
49-
private Long status;
50-
51-
public RootEntity() {
52-
}
53-
54-
public RootEntity(Long id, Long version, String caption, Long status) {
55-
this.id = id;
56-
this.version = version;
57-
this.caption = caption;
58-
this.status = status;
59-
}
60-
61-
public Long getId() {
62-
return id;
63-
}
64-
}
65-
66-
@Test
67-
@TestForIssue(jiraKey = "HHH-12087")
68-
public void testPagination() throws Exception {
69-
39+
@Before
40+
public void setUp() {
7041
doInJPA( this::entityManagerFactory, entityManager -> {
7142
entityManager.persist( new RootEntity( 1L, 7L, "t40", 2L ) );
7243
entityManager.persist( new RootEntity( 16L, 1L, "t47", 2L ) );
@@ -86,12 +57,26 @@ public void testPagination() throws Exception {
8657
entityManager.persist( new RootEntity( 5L, 6L, "t37", 1L ) );
8758
entityManager.persist( new RootEntity( 13L, 1L, "t44", 1L ) );
8859
} );
60+
}
61+
62+
@After
63+
public void tearDown() {
64+
doInJPA( this::entityManagerFactory, entityManager -> {
65+
entityManager.createQuery( "delete from RootEntity" ).executeUpdate();
66+
} );
67+
}
68+
8969

70+
@Test
71+
@TestForIssue(jiraKey = "HHH-12087")
72+
public void testPagination() {
9073
doInJPA( this::entityManagerFactory, entityManager -> {
9174
List<RootEntity> rootEntitiesAllPages = getLimitedRows( entityManager, 0, 10 );
9275

9376
List<RootEntity> rootEntitiesFirst = getLimitedRows( entityManager, 0, 5 );
77+
assertEquals( 5, rootEntitiesFirst.size() );
9478
List<RootEntity> rootEntitiesSecond = getLimitedRows( entityManager, 5, 10 );
79+
assertEquals( 10, rootEntitiesSecond.size() );
9580

9681
assertEquals( rootEntitiesAllPages.get( 0 ).getId(), rootEntitiesFirst.get( 0 ).getId() );
9782
assertEquals( rootEntitiesAllPages.get( 1 ).getId(), rootEntitiesFirst.get( 1 ).getId() );
@@ -107,6 +92,20 @@ public void testPagination() throws Exception {
10792
} );
10893
}
10994

95+
@Test
96+
public void testPaginationWithSetMaxResultsOnly() {
97+
doInJPA( this::entityManagerFactory, entityManager -> {
98+
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
99+
CriteriaQuery<RootEntity> cq = cb.createQuery( RootEntity.class );
100+
Root<RootEntity> c = cq.from( RootEntity.class );
101+
CriteriaQuery<RootEntity> select = cq.select( c ).orderBy( cb.desc( c.get( "status" ) ) );
102+
TypedQuery<RootEntity> typedQuery = entityManager.createQuery( select );
103+
typedQuery.setMaxResults( 10 );
104+
List<RootEntity> resultList = typedQuery.getResultList();
105+
assertEquals( 10, resultList.size() );
106+
} );
107+
}
108+
110109
private List<RootEntity> getAllRows(EntityManager em) {
111110
CriteriaBuilder cb = em.getCriteriaBuilder();
112111
CriteriaQuery<RootEntity> cq = cb.createQuery( RootEntity.class );
@@ -125,7 +124,33 @@ private List<RootEntity> getLimitedRows(EntityManager em, int start, int end) {
125124
return typedQuery.getResultList();
126125
}
127126

128-
private void createRootEntity(EntityManager entityManager, Long id, Long version, String caption, String status) {
127+
@Entity(name = "RootEntity")
128+
@Table(name = "V_MYTABLE_LAST")
129+
public static class RootEntity implements Serializable {
130+
131+
@Id
132+
private Long id;
133+
134+
@Id
135+
private Long version;
136+
137+
private String caption;
138+
139+
private Long status;
140+
141+
public RootEntity() {
142+
}
143+
144+
public RootEntity(Long id, Long version, String caption, Long status) {
145+
this.id = id;
146+
this.version = version;
147+
this.caption = caption;
148+
this.status = status;
149+
}
129150

151+
public Long getId() {
152+
return id;
153+
}
130154
}
155+
131156
}
Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
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

Comments
 (0)