Skip to content

Commit 60459f0

Browse files
committed
HHH-19324 - Switch tests using hbm.xml to use mapping.xml
1 parent 28b495c commit 60459f0

File tree

4 files changed

+100
-135
lines changed

4 files changed

+100
-135
lines changed

hibernate-core/src/test/java/org/hibernate/orm/test/batch/BatchTest.java

Lines changed: 68 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -4,122 +4,109 @@
44
*/
55
package org.hibernate.orm.test.batch;
66

7-
import java.math.BigDecimal;
8-
97
import org.hibernate.CacheMode;
108
import org.hibernate.ScrollMode;
119
import org.hibernate.ScrollableResults;
12-
import org.hibernate.Session;
13-
import org.hibernate.Transaction;
14-
import org.hibernate.cfg.Configuration;
15-
import org.hibernate.cfg.Environment;
10+
import org.hibernate.cfg.AvailableSettings;
11+
import org.hibernate.testing.orm.junit.DomainModel;
12+
import org.hibernate.testing.orm.junit.ServiceRegistry;
13+
import org.hibernate.testing.orm.junit.SessionFactory;
14+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
15+
import org.hibernate.testing.orm.junit.Setting;
16+
import org.junit.jupiter.api.Test;
1617

17-
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
18-
import org.junit.Test;
18+
import java.math.BigDecimal;
19+
import java.math.RoundingMode;
1920

2021
/**
2122
* This is how to do batch processing in Hibernate. Remember to enable JDBC batch updates, or this test will take a
2223
* VeryLongTime!
2324
*
2425
* @author Gavin King
2526
*/
26-
public class BatchTest extends BaseCoreFunctionalTestCase {
27-
28-
@Override
29-
protected String getBaseForMappings() {
30-
return "org/hibernate/orm/test/";
31-
}
32-
33-
@Override
34-
public String[] getMappings() {
35-
return new String[] { "batch/DataPoint.hbm.xml" };
36-
}
37-
38-
@Override
39-
public void configure(Configuration cfg) {
40-
cfg.setProperty( Environment.STATEMENT_BATCH_SIZE, 20 );
41-
}
27+
@SuppressWarnings("JUnitMalformedDeclaration")
28+
@ServiceRegistry(settings = {
29+
@Setting(name= AvailableSettings.STATEMENT_BATCH_SIZE, value = "20")
30+
})
31+
@DomainModel(xmlMappings = "org/hibernate/orm/test/batch/DataPoint.xml")
32+
@SessionFactory
33+
public class BatchTest {
4234

4335
@Test
44-
public void testBatchInsertUpdate() {
36+
public void testBatchInsertUpdate(SessionFactoryScope factoryScope) {
4537
long start = System.currentTimeMillis();
4638
final int N = 5000; //26 secs with batch flush, 26 without
4739
//final int N = 100000; //53 secs with batch flush, OOME without
4840
//final int N = 250000; //137 secs with batch flush, OOME without
49-
int batchSize = sessionFactory().getSessionFactoryOptions().getJdbcBatchSize();
50-
doBatchInsertUpdate( N, batchSize );
41+
int batchSize = factoryScope.getSessionFactory().getSessionFactoryOptions().getJdbcBatchSize();
42+
doBatchInsertUpdate( N, batchSize, factoryScope );
5143
System.out.println( System.currentTimeMillis() - start );
5244
}
5345

5446
@Test
55-
public void testBatchInsertUpdateSizeEqJdbcBatchSize() {
56-
int batchSize = sessionFactory().getSessionFactoryOptions().getJdbcBatchSize();
57-
doBatchInsertUpdate( 50, batchSize );
47+
public void testBatchInsertUpdateSizeEqJdbcBatchSize(SessionFactoryScope factoryScope) {
48+
int batchSize = factoryScope.getSessionFactory().getSessionFactoryOptions().getJdbcBatchSize();
49+
doBatchInsertUpdate( 50, batchSize, factoryScope );
5850
}
5951

6052
@Test
61-
public void testBatchInsertUpdateSizeLtJdbcBatchSize() {
62-
int batchSize = sessionFactory().getSessionFactoryOptions().getJdbcBatchSize();
63-
doBatchInsertUpdate( 50, batchSize - 1 );
53+
public void testBatchInsertUpdateSizeLtJdbcBatchSize(SessionFactoryScope factoryScope) {
54+
int batchSize = factoryScope.getSessionFactory().getSessionFactoryOptions().getJdbcBatchSize();
55+
doBatchInsertUpdate( 50, batchSize - 1, factoryScope );
6456
}
6557

6658
@Test
67-
public void testBatchInsertUpdateSizeGtJdbcBatchSize() {
68-
int batchSize = sessionFactory().getSessionFactoryOptions().getJdbcBatchSize();
69-
doBatchInsertUpdate( 50, batchSize + 1 );
59+
public void testBatchInsertUpdateSizeGtJdbcBatchSize(SessionFactoryScope factoryScope) {
60+
int batchSize = factoryScope.getSessionFactory().getSessionFactoryOptions().getJdbcBatchSize();
61+
doBatchInsertUpdate( 50, batchSize + 1, factoryScope );
7062
}
7163

72-
public void doBatchInsertUpdate(int nEntities, int nBeforeFlush) {
73-
Session s = openSession();
74-
s.setCacheMode( CacheMode.IGNORE );
75-
Transaction t = s.beginTransaction();
76-
for ( int i = 0; i < nEntities; i++ ) {
77-
DataPoint dp = new DataPoint();
78-
dp.setX( new BigDecimal( i * 0.1d ).setScale( 19, BigDecimal.ROUND_DOWN ) );
79-
dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale( 19, BigDecimal.ROUND_DOWN ) );
80-
s.persist( dp );
81-
if ( ( i + 1 ) % nBeforeFlush == 0 ) {
82-
s.flush();
83-
s.clear();
64+
public void doBatchInsertUpdate(int nEntities, int nBeforeFlush, SessionFactoryScope factoryScope) {
65+
factoryScope.inTransaction( (session) -> {
66+
session.setCacheMode( CacheMode.IGNORE );
67+
for ( int i = 0; i < nEntities; i++ ) {
68+
DataPoint dp = new DataPoint();
69+
dp.setX( new BigDecimal( i * 0.1d ).setScale( 19, RoundingMode.DOWN ) );
70+
dp.setY( BigDecimal.valueOf( Math.cos( dp.getX().doubleValue() ) ).setScale( 19, RoundingMode.DOWN ) );
71+
session.persist( dp );
72+
if ( ( i + 1 ) % nBeforeFlush == 0 ) {
73+
session.flush();
74+
session.clear();
75+
}
8476
}
85-
}
86-
t.commit();
87-
s.close();
77+
} );
8878

89-
s = openSession();
90-
s.setCacheMode( CacheMode.IGNORE );
91-
t = s.beginTransaction();
92-
int i = 0;
93-
try (ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" )
94-
.scroll( ScrollMode.FORWARD_ONLY )) {
95-
while ( sr.next() ) {
96-
DataPoint dp = (DataPoint) sr.get();
97-
dp.setDescription( "done!" );
98-
if ( ++i % nBeforeFlush == 0 ) {
99-
s.flush();
100-
s.clear();
79+
factoryScope.inTransaction( (session) -> {
80+
session.setCacheMode( CacheMode.IGNORE );
81+
int i = 0;
82+
try (ScrollableResults sr = session.createQuery( "from DataPoint dp order by dp.x asc" )
83+
.scroll( ScrollMode.FORWARD_ONLY )) {
84+
while ( sr.next() ) {
85+
DataPoint dp = (DataPoint) sr.get();
86+
dp.setDescription( "done!" );
87+
if ( ++i % nBeforeFlush == 0 ) {
88+
session.flush();
89+
session.clear();
90+
}
10191
}
10292
}
103-
}
104-
t.commit();
105-
s.close();
93+
} );
94+
95+
factoryScope.inTransaction( (session) -> {
96+
session.setCacheMode( CacheMode.IGNORE );
97+
int i = 0;
10698

107-
s = openSession();
108-
s.setCacheMode( CacheMode.IGNORE );
109-
t = s.beginTransaction();
110-
i = 0;
111-
try (ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" )
112-
.scroll( ScrollMode.FORWARD_ONLY )) {
113-
while ( sr.next() ) {
114-
DataPoint dp = (DataPoint) sr.get();
115-
s.remove( dp );
116-
if ( ++i % nBeforeFlush == 0 ) {
117-
s.flush();
118-
s.clear();
99+
try (ScrollableResults sr = session.createQuery( "from DataPoint dp order by dp.x asc" )
100+
.scroll( ScrollMode.FORWARD_ONLY )) {
101+
while ( sr.next() ) {
102+
DataPoint dp = (DataPoint) sr.get();
103+
session.remove( dp );
104+
if ( ++i % nBeforeFlush == 0 ) {
105+
session.flush();
106+
session.clear();
107+
}
119108
}
120109
}
121-
}
122-
t.commit();
123-
s.close();
110+
} );
124111
}
125112
}

hibernate-core/src/test/resources/org/hibernate/orm/test/array/A.xml

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,3 @@
3333
</attributes>
3434
</entity>
3535
</entity-mappings>
36-
37-
<!--<hibernate-mapping -->
38-
<!-- package="org.hibernate.orm.test.array">-->
39-
<!-- -->
40-
<!-- <class name="A" lazy="true" table="aaa">-->
41-
<!-- -->
42-
<!-- <id name="id">-->
43-
<!-- <generator class="native"/>-->
44-
<!-- </id>-->
45-
46-
<!-- <array name="bs" cascade="all" fetch="join">-->
47-
<!-- <key column="a_id"/>-->
48-
<!-- <list-index column="idx"/>-->
49-
<!-- <one-to-many class="B"/>-->
50-
<!-- </array>-->
51-
52-
<!-- </class>-->
53-
54-
<!-- <class name="B" lazy="true" table="bbb">-->
55-
<!-- <id name="id">-->
56-
<!-- <generator class="native"/>-->
57-
<!-- </id>-->
58-
<!-- </class>-->
59-
60-
61-
<!--</hibernate-mapping>-->

hibernate-core/src/test/resources/org/hibernate/orm/test/batch/DataPoint.hbm.xml

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
~ SPDX-License-Identifier: Apache-2.0
4+
~ Copyright Red Hat Inc. and Hibernate Authors
5+
-->
6+
<entity-mappings xmlns="http://www.hibernate.org/xsd/orm/mapping"
7+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
8+
version="7.0">
9+
<package>org.hibernate.orm.test.batch</package>
10+
11+
<entity class="DataPoint">
12+
<table>
13+
<unique-constraint name="xy">
14+
<column-name>xval</column-name>
15+
<column-name>yval</column-name>
16+
</unique-constraint>
17+
</table>
18+
<dynamic-update>true</dynamic-update>
19+
<attributes>
20+
<id name="id">
21+
<generated-value generator="increment"/>
22+
</id>
23+
<basic name="description"/>
24+
<basic name="x">
25+
<column name="xval" nullable="false" precision="25" scale="20"/>
26+
</basic>
27+
<basic name="y">
28+
<column name="yval" nullable="false" precision="25" scale="20"/>
29+
</basic>
30+
</attributes>
31+
</entity>
32+
</entity-mappings>

0 commit comments

Comments
 (0)