mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Merge pull request #2770 from ebean-orm/feature/sqlquery-useTransaction
Add SqlQuery usingTransaction() for consistency and deprecate ExtendedServer API
This commit is contained in:
@@ -196,6 +196,23 @@ public class SqlQueryTests extends BaseTestCase {
|
||||
assertThat(minCreated).isBefore(OffsetDateTime.now());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void typeQuery_usingTransaction() {
|
||||
ResetBasicData.reset();
|
||||
|
||||
String sql = "select max(unit_price) from o_order_detail where order_qty > ?";
|
||||
try (Transaction transaction = DB.createTransaction()) {
|
||||
|
||||
BigDecimal maxPrice = DB.sqlQuery(sql)
|
||||
.setParameter(1, 2)
|
||||
.mapToScalar(BigDecimal.class)
|
||||
.usingTransaction(transaction)
|
||||
.findOne();
|
||||
|
||||
assertThat(maxPrice).isNotNull();
|
||||
}
|
||||
}
|
||||
|
||||
static class CustDto {
|
||||
|
||||
long id;
|
||||
@@ -224,6 +241,44 @@ public class SqlQueryTests extends BaseTestCase {
|
||||
|
||||
private static final CustMapper CUST_MAPPER = new CustMapper();
|
||||
|
||||
@Test
|
||||
void queryUsingTransaction() {
|
||||
ResetBasicData.reset();
|
||||
|
||||
try (Transaction txn = DB.createTransaction()) {
|
||||
String sql = "select id, name, status from o_customer where name is not null";
|
||||
AtomicInteger counter = new AtomicInteger();
|
||||
DB.sqlQuery(sql)
|
||||
.usingTransaction(txn)
|
||||
.mapTo(CUST_MAPPER)
|
||||
.findEach(custDto -> {
|
||||
counter.incrementAndGet();
|
||||
assertThat(custDto.name).isNotNull();
|
||||
});
|
||||
|
||||
assertThat(counter.get()).isGreaterThan(0);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void mapperUsingTransaction() {
|
||||
ResetBasicData.reset();
|
||||
|
||||
try (Transaction txn = DB.createTransaction()) {
|
||||
String sql = "select id, name, status from o_customer where name is not null";
|
||||
AtomicInteger counter = new AtomicInteger();
|
||||
DB.sqlQuery(sql)
|
||||
.mapTo(CUST_MAPPER)
|
||||
.usingTransaction(txn)
|
||||
.findEach(custDto -> {
|
||||
counter.incrementAndGet();
|
||||
assertThat(custDto.name).isNotNull();
|
||||
});
|
||||
|
||||
assertThat(counter.get()).isGreaterThan(0);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findEach_mapper() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user