mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-12 22:41:35 +00:00
Refactor move tests from ebean-core to ebean-test
This commit is contained in:
1 parent
0b00a14fd3
commit
eab56d323b
1650 files changed
+185
-147
No files matched your search
@@ -0,0 +1,333 @@
|
||||
package org.tests.batchinsert;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.DtoQuery2Test;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebean.annotation.IgnorePlatform;
|
||||
import io.ebean.annotation.PersistBatch;
|
||||
import io.ebean.annotation.Platform;
|
||||
import io.ebean.annotation.Transactional;
|
||||
import io.ebean.meta.MetaTimedMetric;
|
||||
import io.ebean.meta.ServerMetrics;
|
||||
import io.ebeaninternal.api.SpiTransaction;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.EBasicVer;
|
||||
import org.tests.model.basic.TSDetail;
|
||||
import org.tests.model.basic.TSMaster;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
public class TestBatchInsertFlush extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void no_cascade() {
|
||||
|
||||
Database server = DB.getDefault();
|
||||
|
||||
resetAllMetrics();
|
||||
|
||||
Transaction transaction = server.beginTransaction();
|
||||
try {
|
||||
transaction.setPersistCascade(false);
|
||||
transaction.setBatchSize(10);
|
||||
transaction.setBatchMode(true);
|
||||
transaction.setLabel("TestBatchInsertFlush.no_cascade");
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
TSMaster m = new TSMaster();
|
||||
m.setName("master1");
|
||||
server.save(m);
|
||||
|
||||
// we don't want this to flush batch yet but instead
|
||||
// determine its batch depth based on imported assoc to master
|
||||
TSDetail d1 = new TSDetail("d1");
|
||||
d1.setMaster(m);
|
||||
server.save(d1);
|
||||
|
||||
TSDetail d2 = new TSDetail("d2");
|
||||
d2.setMaster(m);
|
||||
server.save(d2);
|
||||
|
||||
// we want this to batch flush with master 1
|
||||
TSMaster m2 = new TSMaster();
|
||||
m2.setName("master2");
|
||||
server.save(m2);
|
||||
|
||||
transaction.commit();
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
|
||||
// we get the 2 master inserts first
|
||||
assertSql(sql.get(0)).contains("insert into t_atable_thatisrelatively");
|
||||
assertSql(sql.get(1)).contains("-- bind(");
|
||||
// detail
|
||||
assertThat(sql.get(3)).contains("insert into t_detail_with_other_namexxxyy");
|
||||
|
||||
assertThat(((SpiTransaction)transaction).getLabel()).isEqualTo("TestBatchInsertFlush.no_cascade");
|
||||
|
||||
} finally {
|
||||
transaction.end();
|
||||
}
|
||||
|
||||
ServerMetrics metrics = collectMetrics();
|
||||
List<MetaTimedMetric> txnStats = metrics.timedMetrics();
|
||||
for (MetaTimedMetric txnMetric : txnStats) {
|
||||
System.out.println(txnMetric);
|
||||
}
|
||||
assertThat(txnStats).hasSize(4);
|
||||
assertThat(txnStats.get(0).name()).isEqualTo("txn.main");
|
||||
assertThat(txnStats.get(1).name()).isEqualTo("txn.named.TestBatchInsertFlush.no_cascade");
|
||||
assertThat(txnStats.get(2).name()).isEqualTo("iud.TSDetail.insertBatch");
|
||||
assertThat(txnStats.get(3).name()).isEqualTo("iud.TSMaster.insertBatch");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional(batch = PersistBatch.ALL, flushOnQuery = false)
|
||||
public void transactional_flushOnQueryFalse() {
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
DB.save(new EBasicVer("b1"));
|
||||
DB.save(new EBasicVer("b2"));
|
||||
|
||||
// does not trigger JDBC batch with flushOnQuery = false
|
||||
DB.find(Customer.class).findCount();
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
assertSql(sql.get(0)).contains("select count(*)");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional(batchSize = 20)
|
||||
public void transactional_flushOnSqlQuery() {
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
DB.save(new EBasicVer("b1"));
|
||||
DB.save(new EBasicVer("b2"));
|
||||
|
||||
// trigger JDBC batch by default
|
||||
DB.sqlQuery("select count(*) from e_basicver")
|
||||
.mapToScalar(Integer.class)
|
||||
.findOne();
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
assertSql(sql.get(0)).contains("insert into e_basicver");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional(batchSize = 20)
|
||||
public void transactional_flushOnDtoQuery() {
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
DB.save(new EBasicVer("b1"));
|
||||
DB.save(new EBasicVer("b2"));
|
||||
|
||||
// trigger JDBC batch by default
|
||||
DB.findDto(DtoQuery2Test.DCust.class, "select id, name from o_customer")
|
||||
.findList();
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
assertSql(sql.get(0)).contains("insert into e_basicver");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional(batch = PersistBatch.ALL)
|
||||
public void transactional_flushOnQuery() {
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
DB.save(new EBasicVer("b1"));
|
||||
DB.save(new EBasicVer("b2"));
|
||||
|
||||
// by default triggers flush of JDBC batch
|
||||
DB.find(Customer.class).findCount();
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
assertSql(sql.get(0)).contains("insert into e_basicver");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional(batch = PersistBatch.ALL)
|
||||
@IgnorePlatform({Platform.SQLSERVER, Platform.HANA}) // has generated IDs
|
||||
public void transactional_flushOnGetId() {
|
||||
|
||||
Database server = DB.getDefault();
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
EBasicVer b1 = new EBasicVer("b1");
|
||||
server.save(b1);
|
||||
|
||||
EBasicVer b2 = new EBasicVer("b2");
|
||||
server.save(b2);
|
||||
|
||||
//flush here
|
||||
assertThat(LoggedSqlCollector.current()).isEmpty();
|
||||
Integer id = b1.getId();
|
||||
assertNotNull(id);
|
||||
assertThat(LoggedSqlCollector.current()).hasSize(3);
|
||||
|
||||
EBasicVer b3 = new EBasicVer("b3");
|
||||
server.save(b3);
|
||||
}
|
||||
|
||||
@Test
|
||||
@IgnorePlatform({Platform.SQLSERVER, Platform.HANA})
|
||||
public void testFlushOnGetId() {
|
||||
|
||||
Database server = DB.getDefault();
|
||||
Transaction txn = server.beginTransaction();
|
||||
try {
|
||||
LoggedSqlCollector.start();
|
||||
txn.setBatchMode(true);
|
||||
|
||||
EBasicVer b1 = new EBasicVer("b1");
|
||||
server.save(b1, txn);
|
||||
|
||||
EBasicVer b2 = new EBasicVer("b2");
|
||||
server.save(b2, txn);
|
||||
|
||||
//flush here
|
||||
assertThat(LoggedSqlCollector.current()).isEmpty();
|
||||
Integer id = b1.getId();
|
||||
assertNotNull(id);
|
||||
assertThat(LoggedSqlCollector.current()).hasSize(3);
|
||||
|
||||
EBasicVer b3 = new EBasicVer("b3");
|
||||
server.save(b3, txn);
|
||||
|
||||
txn.commit();
|
||||
|
||||
} finally {
|
||||
txn.end();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional(batch = PersistBatch.ALL)
|
||||
public void transactional_noflushWhenIdIsLoaded() {
|
||||
|
||||
Database server = DB.getDefault();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
EBasicVer b1 = new EBasicVer("b1");
|
||||
b1.setId(78965);
|
||||
server.save(b1);
|
||||
|
||||
EBasicVer b2 = new EBasicVer("b2");
|
||||
b2.setId(78645);
|
||||
server.save(b2);
|
||||
|
||||
assertThat(LoggedSqlCollector.current()).isEmpty();
|
||||
// dont flush here
|
||||
Integer id = b1.getId();
|
||||
assertNotNull(id);
|
||||
assertThat(LoggedSqlCollector.current()).isEmpty();
|
||||
|
||||
EBasicVer b3 = new EBasicVer("b3");
|
||||
server.save(b3);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@IgnorePlatform({Platform.SQLSERVER, Platform.ORACLE})
|
||||
public void noflushWhenIdIsLoaded() {
|
||||
|
||||
Database server = DB.getDefault();
|
||||
Transaction txn = server.beginTransaction();
|
||||
try {
|
||||
LoggedSqlCollector.start();
|
||||
txn.setBatchMode(true);
|
||||
|
||||
EBasicVer b1 = new EBasicVer("b1");
|
||||
b1.setId(546864);
|
||||
server.save(b1, txn);
|
||||
|
||||
EBasicVer b2 = new EBasicVer("b2");
|
||||
b2.setId(21354);
|
||||
server.save(b2, txn);
|
||||
|
||||
assertThat(LoggedSqlCollector.current()).isEmpty();
|
||||
//dont flush here
|
||||
Integer id = b1.getId();
|
||||
assertNotNull(id);
|
||||
assertThat(LoggedSqlCollector.current()).isEmpty();
|
||||
|
||||
EBasicVer b3 = new EBasicVer("b3");
|
||||
server.save(b3, txn);
|
||||
|
||||
txn.commit();
|
||||
assertThat(LoggedSqlCollector.current()).hasSize(5);
|
||||
|
||||
} finally {
|
||||
txn.end();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFlushOnGetProperty() {
|
||||
|
||||
Database server = DB.getDefault();
|
||||
Transaction txn = server.beginTransaction();
|
||||
try {
|
||||
txn.setBatchMode(true);
|
||||
|
||||
EBasicVer b1 = new EBasicVer("b1");
|
||||
server.save(b1, txn);
|
||||
|
||||
EBasicVer b2 = new EBasicVer("b2");
|
||||
server.save(b2, txn);
|
||||
|
||||
// flush here
|
||||
Timestamp lastUpdate = b1.getLastUpdate();
|
||||
assertNotNull(lastUpdate);
|
||||
|
||||
EBasicVer b3 = new EBasicVer("b3");
|
||||
server.save(b3, txn);
|
||||
|
||||
txn.commit();
|
||||
|
||||
} finally {
|
||||
txn.end();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFlushOnSetProperty() {
|
||||
|
||||
Database server = DB.getDefault();
|
||||
Transaction txn = server.beginTransaction();
|
||||
try {
|
||||
txn.setBatchMode(true);
|
||||
|
||||
EBasicVer b1 = new EBasicVer("b1");
|
||||
server.save(b1, txn);
|
||||
|
||||
EBasicVer b2 = new EBasicVer("b2");
|
||||
server.save(b2, txn);
|
||||
|
||||
// flush here
|
||||
b1.setDescription("modify");
|
||||
|
||||
EBasicVer b3 = new EBasicVer("b3");
|
||||
server.save(b3, txn);
|
||||
|
||||
txn.commit();
|
||||
|
||||
} finally {
|
||||
txn.end();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package org.tests.batchinsert;
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.annotation.PersistBatch;
|
||||
import io.ebean.annotation.Transactional;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.batchinsert.o2o.MeterAddressData;
|
||||
import org.tests.batchinsert.o2o.MeterContractData;
|
||||
import org.tests.batchinsert.o2o.MeterSpecialNeedsClient;
|
||||
import org.tests.batchinsert.o2o.MeterSpecialNeedsContact;
|
||||
import org.tests.batchinsert.o2o.MeterVersion;
|
||||
|
||||
public class TestBatchInsertOneToOne {
|
||||
private MeterVersion build(){
|
||||
MeterAddressData addressData = new MeterAddressData();
|
||||
addressData.setStreet("street");
|
||||
MeterContractData contractData = new MeterContractData();
|
||||
|
||||
MeterSpecialNeedsClient specialNeedsClient = new MeterSpecialNeedsClient();
|
||||
contractData.setSpecialNeedsClient(specialNeedsClient);
|
||||
|
||||
MeterSpecialNeedsContact contact1 = new MeterSpecialNeedsContact();
|
||||
|
||||
specialNeedsClient.setPrimary(contact1);
|
||||
|
||||
MeterVersion meterVersion = new MeterVersion();
|
||||
meterVersion.setAddressData(addressData);
|
||||
meterVersion.setContractData(contractData);
|
||||
|
||||
return meterVersion;
|
||||
}
|
||||
|
||||
@Transactional(batch = PersistBatch.ALL, batchOnCascade = PersistBatch.ALL, batchSize = 10)
|
||||
@Test
|
||||
public void test() {
|
||||
MeterVersion meterVersion = build();
|
||||
|
||||
DB.save(meterVersion);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,229 @@
|
||||
package org.tests.batchinsert;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebean.annotation.IgnorePlatform;
|
||||
import io.ebean.annotation.PersistBatch;
|
||||
import io.ebean.annotation.Platform;
|
||||
import io.ebean.annotation.Transactional;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.UTDetail;
|
||||
import org.tests.model.basic.UTMaster;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
//import static org.assertj.core.api.Assertions.assertThat;
|
||||
//import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
public class TestBatchInsertSimple extends BaseTestCase {
|
||||
|
||||
Random random = new Random();
|
||||
|
||||
@Test
|
||||
public void testJdbcBatchPerRequestWithMasterAndDetails() {
|
||||
|
||||
int numOfMasters = 4;// 2 + random.nextInt(8);
|
||||
|
||||
Transaction transaction = DB.beginTransaction();
|
||||
try {
|
||||
transaction.setBatchMode(false);
|
||||
transaction.setBatchOnCascade(true);
|
||||
transaction.setBatchSize(30);
|
||||
// setBatchGetGeneratedKeys MUST be turned off for MS SQL Server because :(
|
||||
transaction.setGetGeneratedKeys(false);
|
||||
|
||||
for (int i = 0; i < numOfMasters; i++) {
|
||||
UTMaster master = createMasterAndDetails(i, 20);
|
||||
master.save();
|
||||
}
|
||||
|
||||
transaction.commit();
|
||||
|
||||
} finally {
|
||||
transaction.end();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@IgnorePlatform(Platform.HANA)
|
||||
public void testTransactional() {
|
||||
|
||||
saveWithFullBatchMode();
|
||||
}
|
||||
|
||||
@Transactional(batchSize = 50)
|
||||
public void saveWithFullBatchMode() {
|
||||
|
||||
int numOfMasters = 4;
|
||||
|
||||
for (int i = 0; i < numOfMasters; i++) {
|
||||
UTMaster master = createMasterAndDetails(i, 5);
|
||||
// the save is 'batched' and does not execute immediately
|
||||
// ... it now acts more like 'merge/persist'
|
||||
master.save();
|
||||
}
|
||||
}
|
||||
|
||||
// @Test
|
||||
// public void testTransactional_skipGeneratedBeans() {
|
||||
//
|
||||
// if (isMsSqlServer()) return;
|
||||
//
|
||||
// List<UTMaster> beans = saveWithFullBatchMode_skipGeneratedKeys();
|
||||
// for (UTMaster bean : beans) {
|
||||
// assertNull(bean.getId());
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// @Transactional(batch=PersistBatch.ALL, batchSize=50, getGeneratedKeys = false)
|
||||
// public List<UTMaster> saveWithFullBatchMode_skipGeneratedKeys() {
|
||||
//
|
||||
// Transaction transaction = server().currentTransaction();
|
||||
// SpiTransaction spiTxn = (SpiTransaction)transaction;
|
||||
// Boolean generatedKeys = spiTxn.getBatchGetGeneratedKeys();
|
||||
//
|
||||
// assertThat(generatedKeys).isFalse();
|
||||
//
|
||||
// List<UTMaster> beans = new ArrayList<UTMaster>();
|
||||
// for (int i = 0; i < 4; i++) {
|
||||
// beans.add(createMaster(i));
|
||||
// }
|
||||
//
|
||||
// server().saveAll(beans);
|
||||
// return beans;
|
||||
// }
|
||||
|
||||
@Test
|
||||
public void testJdbcBatchPerRequestWithMasterOnly() {
|
||||
|
||||
int numOfMasters = 4;
|
||||
|
||||
Transaction transaction = DB.beginTransaction();
|
||||
try {
|
||||
transaction.setBatchMode(false);
|
||||
transaction.setBatchOnCascade(true);
|
||||
transaction.setBatchSize(30);
|
||||
// setBatchGetGeneratedKeys MUST be turned off for MS SQL Server because :(
|
||||
transaction.setGetGeneratedKeys(false);
|
||||
|
||||
for (int i = 0; i < numOfMasters; i++) {
|
||||
UTMaster master = createMaster(i);
|
||||
DB.save(master);
|
||||
}
|
||||
|
||||
transaction.commit();
|
||||
|
||||
} finally {
|
||||
transaction.end();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJdbcBatchOnCollection() {
|
||||
|
||||
int numOfMasters = 3;
|
||||
|
||||
List<UTMaster> masters = new ArrayList<>();
|
||||
for (int i = 0; i < numOfMasters; i++) {
|
||||
masters.add(createMasterAndDetails(i, 7));
|
||||
}
|
||||
|
||||
Transaction transaction = DB.beginTransaction();
|
||||
try {
|
||||
transaction.setBatchMode(true);
|
||||
transaction.setBatchOnCascade(PersistBatch.ALL.equals(spiEbeanServer().databasePlatform().getPersistBatchOnCascade()));
|
||||
transaction.setBatchSize(20);
|
||||
|
||||
// escalate based on batchOnCascade value
|
||||
DB.saveAll(masters);
|
||||
|
||||
transaction.commit();
|
||||
|
||||
} finally {
|
||||
transaction.end();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJdbcBatchOnCollectionNoTransaction() {
|
||||
|
||||
int numOfMasters = 3;
|
||||
|
||||
List<UTMaster> masters = new ArrayList<>();
|
||||
for (int i = 0; i < numOfMasters; i++) {
|
||||
masters.add(createMasterAndDetails(i, 5));
|
||||
}
|
||||
|
||||
// escalate based on batchOnCascade value
|
||||
DB.saveAll(masters);
|
||||
|
||||
for (int i = 0; i < masters.size(); i++) {
|
||||
UTMaster utMaster = masters.get(i);
|
||||
utMaster.setName(utMaster.getName() + "-Mod");
|
||||
if (i % 2 == 0) {
|
||||
// make the updates a little bit different
|
||||
utMaster.setDescription("Blah");
|
||||
}
|
||||
}
|
||||
|
||||
DB.saveAll(masters);
|
||||
}
|
||||
|
||||
private UTMaster createMasterAndDetails(int masterPos, int size) {
|
||||
|
||||
UTMaster master = createMaster(masterPos);
|
||||
List<UTDetail> details = new ArrayList<>();
|
||||
|
||||
int count = 2 + random.nextInt(size);
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
|
||||
int qty = 1 + random.nextInt(99);
|
||||
double amount = random.nextDouble();
|
||||
|
||||
details.add(createDetail(masterPos + "-" + i, qty, amount));
|
||||
}
|
||||
master.setDetails(details);
|
||||
return master;
|
||||
}
|
||||
|
||||
private UTMaster createMaster(int position) {
|
||||
UTMaster m = new UTMaster();
|
||||
m.setName("batchInsert-master" + position);
|
||||
return m;
|
||||
}
|
||||
|
||||
private UTDetail createDetail(String position, int qty, double amount) {
|
||||
|
||||
UTDetail detail = new UTDetail();
|
||||
detail.setName("batchInsert-detail-" + position);
|
||||
detail.setQty(qty);
|
||||
detail.setAmount(amount);
|
||||
|
||||
// System.out.println("-- "+detail);
|
||||
|
||||
return detail;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void npe_addBatch_withoutAnyBindParams() {
|
||||
try (Transaction txn = DB.beginTransaction()) {
|
||||
// don't write code like this please ...
|
||||
DB.sqlUpdate("update ut_master set name='DoNotDoThisPlease' where id=999999999").addBatch();
|
||||
txn.commit();
|
||||
}
|
||||
|
||||
// don't write code like the above but use bind values like:
|
||||
try (Transaction txn = DB.beginTransaction()) {
|
||||
DB.sqlUpdate("update ut_master set name=? where id=?")
|
||||
.setParameters("DoNotDoThisPlease", 999999999)
|
||||
.addBatch();
|
||||
|
||||
txn.commit();
|
||||
}
|
||||
}
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package org.tests.batchinsert;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Transaction;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.OCachedBean;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestBatchInsertWithInitialisedCollection extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
|
||||
List<OCachedBean> list = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < 3; i++) {
|
||||
OCachedBean bean = new OCachedBean();
|
||||
bean.setName("name " + i);
|
||||
list.add(bean);
|
||||
}
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
Transaction txn = DB.beginTransaction();
|
||||
try {
|
||||
txn.setBatchMode(true);
|
||||
|
||||
DB.saveAll(list);
|
||||
txn.commit();
|
||||
|
||||
} finally {
|
||||
txn.end();
|
||||
}
|
||||
|
||||
List<String> loggedSql = LoggedSqlCollector.stop();
|
||||
assertThat(loggedSql).hasSize(4);
|
||||
assertThat(loggedSql.get(0)).contains("insert into o_cached_bean (");
|
||||
assertThat(loggedSql.get(0)).contains("name) values (?");
|
||||
}
|
||||
}
|
||||
+116
@@ -0,0 +1,116 @@
|
||||
package org.tests.batchinsert;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Database;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebean.annotation.IgnorePlatform;
|
||||
import io.ebean.annotation.PersistBatch;
|
||||
import io.ebean.annotation.Platform;
|
||||
import io.ebeaninternal.api.SpiTransaction;
|
||||
import io.ebeaninternal.server.persist.BatchControl;
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.EBasicWithUniqueCon;
|
||||
import org.tests.model.basic.EOptOneB;
|
||||
import org.tests.model.basic.EOptOneC;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Savepoint;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestBatchOnCascadeExceptionHandling extends BaseTestCase {
|
||||
|
||||
@IgnorePlatform({Platform.HANA, Platform.NUODB})
|
||||
@Test
|
||||
public void testBatchScenarioWithSavepoint() throws SQLException {
|
||||
server().save(createEntityWithName("conflict", "before"));
|
||||
|
||||
Transaction txn = server().beginTransaction();
|
||||
try {
|
||||
EBasicWithUniqueCon v2 = createEntityWithName("conflict", "after");
|
||||
txn.flush();
|
||||
Savepoint sp = txn.connection().setSavepoint();
|
||||
try {
|
||||
server().save(v2); // unique key violation
|
||||
} catch (PersistenceException e) {
|
||||
txn.connection().rollback(sp);
|
||||
|
||||
EBasicWithUniqueCon conflicting = server().find(EBasicWithUniqueCon.class).where().eq("name", "conflict").findOne();
|
||||
assertThat(conflicting).isNotNull();
|
||||
server().delete(conflicting);
|
||||
server().save(v2); // try again
|
||||
}
|
||||
txn.commit();
|
||||
} finally {
|
||||
txn.end();
|
||||
}
|
||||
|
||||
EBasicWithUniqueCon winner = server().find(EBasicWithUniqueCon.class).where().eq("name", "conflict").findOne();
|
||||
assertThat(winner).isNotNull();
|
||||
assertThat(winner.getDescription()).isEqualTo("after");
|
||||
}
|
||||
|
||||
@IgnorePlatform(Platform.NUODB)
|
||||
@Test
|
||||
public void testBatchedInsertFailure() {
|
||||
server().save(createEntityWithName("foo"));
|
||||
testBatchOnCascadeIsExceptionSafe(server(), () -> {
|
||||
server().save(createEntityWithName("foo")); // duplicate name on insert
|
||||
});
|
||||
}
|
||||
|
||||
@IgnorePlatform(Platform.NUODB)
|
||||
@Test
|
||||
public void testBatchedUpdateFailure() {
|
||||
server().save(createEntityWithName("bla"));
|
||||
final EBasicWithUniqueCon bar = createEntityWithName("bar");
|
||||
server().save(bar);
|
||||
testBatchOnCascadeIsExceptionSafe(server(), () -> {
|
||||
bar.setName("bla");
|
||||
server().save(bar); // duplicate name on update
|
||||
});
|
||||
}
|
||||
|
||||
@IgnorePlatform(Platform.NUODB)
|
||||
@Test
|
||||
public void testBatchedDeleteFailure() {
|
||||
final EOptOneC c = new EOptOneC();
|
||||
server().save(c);
|
||||
final EOptOneB b = new EOptOneB();
|
||||
b.setC(c);
|
||||
server().save(b);
|
||||
testBatchOnCascadeIsExceptionSafe(server(), () -> {
|
||||
server().delete(c); // foreign key violation
|
||||
});
|
||||
}
|
||||
|
||||
protected void testBatchOnCascadeIsExceptionSafe(Database server, Runnable failingOperation) {
|
||||
Transaction txn = server.beginTransaction();
|
||||
try {
|
||||
assertThat(txn.isBatchMode()).isFalse();
|
||||
assertThat(txn.isBatchOnCascade()).isSameAs(PersistBatch.ALL.equals(spiEbeanServer().databasePlatform().getPersistBatchOnCascade()));
|
||||
|
||||
failingOperation.run();
|
||||
Assertions.fail("PersistenceException expected");
|
||||
} catch (PersistenceException e) {
|
||||
assertThat(txn.isBatchMode()).as("batch mode").isFalse(); // should not have changed
|
||||
BatchControl bc = ((SpiTransaction) txn).getBatchControl();
|
||||
assertThat(bc == null || bc.isEmpty()).as("batch emtpy").isTrue();
|
||||
} finally {
|
||||
txn.end();
|
||||
}
|
||||
}
|
||||
|
||||
protected EBasicWithUniqueCon createEntityWithName(String name) {
|
||||
return createEntityWithName(name, null);
|
||||
}
|
||||
|
||||
protected EBasicWithUniqueCon createEntityWithName(String name, String description) {
|
||||
EBasicWithUniqueCon it = new EBasicWithUniqueCon();
|
||||
it.setName(name);
|
||||
it.setDescription(description);
|
||||
return it;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package org.tests.batchinsert;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import io.ebean.annotation.IgnorePlatform;
|
||||
import io.ebean.annotation.Platform;
|
||||
import io.ebean.annotation.Transactional;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
public class TestBatchSaveWithGetBeanId extends BaseTestCase {
|
||||
|
||||
/**
|
||||
* Making this transaction with batchSize means that the insert
|
||||
* below does not occur immediately ... and the getBeanId()
|
||||
* should invoke the flush (and hence trigger the insert).
|
||||
*/
|
||||
@Transactional(batchSize = 10)
|
||||
@Test
|
||||
@IgnorePlatform(Platform.HANA) // HANA doesn't support insert batching
|
||||
public void test() {
|
||||
|
||||
Database server = DB.getDefault();
|
||||
Customer model = new Customer();
|
||||
model.setName("foo");
|
||||
|
||||
server.insert(model);
|
||||
|
||||
// should invoke a flush which then means the
|
||||
// insert occurs and the bean has an Id value
|
||||
Object beanId = server.beanId(model);
|
||||
assertNotNull(beanId);
|
||||
|
||||
DB.delete(Customer.class, beanId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package org.tests.batchinsert.o2o;
|
||||
|
||||
import io.ebean.annotation.NotNull;
|
||||
import java.util.UUID;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class MeterAddressData {
|
||||
@Id
|
||||
private UUID id;
|
||||
|
||||
@NotNull
|
||||
private String street;
|
||||
|
||||
public String getStreet() {
|
||||
return street;
|
||||
}
|
||||
|
||||
public void setStreet(String street) {
|
||||
this.street = street;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package org.tests.batchinsert.o2o;
|
||||
|
||||
import io.ebean.annotation.NotNull;
|
||||
import java.util.UUID;
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToOne;
|
||||
|
||||
@Entity
|
||||
public class MeterContractData {
|
||||
@Id
|
||||
private UUID id;
|
||||
|
||||
@NotNull
|
||||
@OneToOne(cascade = CascadeType.ALL)
|
||||
private MeterSpecialNeedsClient specialNeedsClient;
|
||||
|
||||
public MeterSpecialNeedsClient getSpecialNeedsClient() {
|
||||
return specialNeedsClient;
|
||||
}
|
||||
|
||||
public void setSpecialNeedsClient(MeterSpecialNeedsClient specialNeedsClient) {
|
||||
this.specialNeedsClient = specialNeedsClient;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package org.tests.batchinsert.o2o;
|
||||
|
||||
import java.util.UUID;
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToOne;
|
||||
|
||||
@Entity
|
||||
public class MeterSpecialNeedsClient {
|
||||
@Id
|
||||
private UUID id;
|
||||
|
||||
private String name;
|
||||
|
||||
@OneToOne(cascade = CascadeType.ALL)
|
||||
private MeterSpecialNeedsContact primary;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public MeterSpecialNeedsContact getPrimary() {
|
||||
return primary;
|
||||
}
|
||||
|
||||
public void setPrimary(MeterSpecialNeedsContact primary) {
|
||||
this.primary = primary;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package org.tests.batchinsert.o2o;
|
||||
|
||||
import java.util.UUID;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class MeterSpecialNeedsContact {
|
||||
@Id
|
||||
private UUID id;
|
||||
|
||||
private String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package org.tests.batchinsert.o2o;
|
||||
|
||||
import io.ebean.annotation.NotNull;
|
||||
import java.util.UUID;
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToOne;
|
||||
|
||||
@Entity
|
||||
public class MeterVersion {
|
||||
@Id
|
||||
private UUID id;
|
||||
|
||||
//@NotNull
|
||||
@OneToOne(cascade = CascadeType.ALL)
|
||||
private MeterAddressData addressData;
|
||||
|
||||
@NotNull
|
||||
@OneToOne(cascade = CascadeType.ALL)
|
||||
private MeterContractData contractData;
|
||||
|
||||
public MeterAddressData getAddressData() {
|
||||
return addressData;
|
||||
}
|
||||
|
||||
public void setAddressData(MeterAddressData addressData) {
|
||||
this.addressData = addressData;
|
||||
}
|
||||
|
||||
public MeterContractData getContractData() {
|
||||
return contractData;
|
||||
}
|
||||
|
||||
public void setContractData(MeterContractData contractData) {
|
||||
this.contractData = contractData;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user