Skip to content
This repository was archived by the owner on Oct 13, 2020. It is now read-only.

Commit 8f63b79

Browse files
committed
#69 - ENH: Query bean constructor taking Transaction
1 parent 7d3066b commit 8f63b79

5 files changed

Lines changed: 47 additions & 170 deletions

File tree

h2-create.sql

Lines changed: 0 additions & 131 deletions
This file was deleted.

h2-drop.sql

Lines changed: 0 additions & 36 deletions
This file was deleted.

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
<dependency>
3131
<groupId>io.ebean</groupId>
3232
<artifactId>querybean-generator</artifactId>
33-
<version>12.1.11</version>
33+
<version>12.1.12-SNAPSHOT</version>
3434
<scope>provided</scope>
3535
</dependency>
3636

src/main/java/io/ebean/typequery/TQRootBean.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,22 @@ public TQRootBean(Class<T> beanType, Database database) {
137137
this(database.find(beanType));
138138
}
139139

140+
/**
141+
* Construct with a transaction.
142+
*/
143+
protected TQRootBean(Class<T> beanType, Transaction transaction) {
144+
this(beanType);
145+
query.usingTransaction(transaction);
146+
}
147+
148+
/**
149+
* Construct with a database and transaction.
150+
*/
151+
protected TQRootBean(Class<T> beanType, Database database, Transaction transaction) {
152+
this(beanType, database);
153+
query.usingTransaction(transaction);
154+
}
155+
140156
/**
141157
* Construct using a query.
142158
*/

src/test/java/org/querytest/QCustomerTest.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.querytest;
22

33
import io.ebean.DB;
4+
import io.ebean.Database;
45
import io.ebean.PagedList;
56
import io.ebean.Query;
67
import io.ebean.QueryIterator;
@@ -43,6 +44,33 @@
4344

4445
public class QCustomerTest {
4546

47+
@Test
48+
public void findWithTransaction() {
49+
50+
final Database database = DB.getDefault();
51+
52+
try (Transaction txn = database.createTransaction()) {
53+
Customer customer = new Customer();
54+
customer.setName("explicitTransaction");
55+
56+
database.save(customer, txn);
57+
58+
final Customer found = new QCustomer(txn)
59+
.name.eq("explicitTransaction")
60+
.findOne();
61+
assertThat(found).isNotNull();
62+
63+
// not found using other transaction
64+
final Customer foundNot = new QCustomer()
65+
.name.eq("explicitTransaction")
66+
.findOne();
67+
assertThat(foundNot).isNull();
68+
69+
txn.commit();
70+
}
71+
72+
}
73+
4674
@Test
4775
public void findSingleAttribute() {
4876

@@ -83,7 +111,7 @@ public void findIterate() {
83111
try {
84112
while (iterate.hasNext()) {
85113
Customer customer = iterate.next();
86-
customer.getName();
114+
assertThat(customer.getName()).isNotNull();
87115
}
88116
} finally {
89117
iterate.close();
@@ -325,7 +353,7 @@ public void testFindLargeStream() {
325353
try (Stream<Customer> stream = new QCustomer()
326354
.name.startsWith("largeStream")
327355
.id.asc()
328-
.findSteam()) {
356+
.findLargeStream()) {
329357

330358
stream.forEach(it -> sb.add(it.getName()));
331359
}

0 commit comments

Comments
 (0)