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

Commit 1623f70

Browse files
committed
#66 - ENH: Support fetchCache()
1 parent c2a3927 commit 1623f70

5 files changed

Lines changed: 120 additions & 5 deletions

File tree

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323
<dependency>
2424
<groupId>io.ebean</groupId>
2525
<artifactId>ebean</artifactId>
26-
<version>11.43.1</version>
26+
<version>11.43.2</version>
2727
<scope>provided</scope>
2828
</dependency>
2929

3030
<dependency>
3131
<groupId>io.ebean</groupId>
3232
<artifactId>querybean-generator</artifactId>
33-
<version>11.41.1</version>
33+
<version>11.43.2</version>
3434
<scope>provided</scope>
3535
</dependency>
3636

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ public R fetchQuery() {
4444
return _root;
4545
}
4646

47+
/**
48+
* Eagerly fetch this association using L2 bean cache.
49+
* Cache misses are populated via fetchQuery().
50+
*/
51+
public R fetchCache() {
52+
((TQRootBean) _root).query().fetchCache(_name);
53+
return _root;
54+
}
55+
4756
/**
4857
* Use lazy loading for fetching this association.
4958
*/
@@ -68,6 +77,15 @@ public R fetchQuery(String properties) {
6877
return _root;
6978
}
7079

80+
/**
81+
* Eagerly fetch this association using L2 cache with the properties specified.
82+
* Cache misses are populated via fetchQuery().
83+
*/
84+
public R fetchCache(String properties) {
85+
((TQRootBean) _root).query().fetchCache(_name, properties);
86+
return _root;
87+
}
88+
7189
/**
7290
* Deprecated in favor of fetch().
7391
*
@@ -95,6 +113,15 @@ protected final R fetchQueryProperties(TQProperty<?>... props) {
95113
return _root;
96114
}
97115

116+
/**
117+
* Eagerly fetch this association using L2 bean cache.
118+
*/
119+
@SafeVarargs
120+
protected final R fetchCacheProperties(TQProperty<?>... props) {
121+
((TQRootBean) _root).query().fetchCache(_name, properties(props));
122+
return _root;
123+
}
124+
98125
/**
99126
* Eagerly fetch query this association fetching some of the properties.
100127
*/

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

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public TQRootBean(Class<T> beanType, Database database) {
142142
@SuppressWarnings("unchecked")
143143
public TQRootBean(Query<T> query) {
144144
this.query = query;
145-
this.root = (R)this;
145+
this.root = (R) this;
146146
}
147147

148148
/**
@@ -157,7 +157,7 @@ public TQRootBean(boolean aliasDummy) {
157157
* Return the fetch group.
158158
*/
159159
public FetchGroup<T> buildFetchGroup() {
160-
return ((SpiFetchGroupQuery)query()).buildFetchGroup();
160+
return ((SpiFetchGroupQuery) query()).buildFetchGroup();
161161
}
162162

163163
/**
@@ -314,6 +314,26 @@ public R fetchQuery(String path) {
314314
return root;
315315
}
316316

317+
/**
318+
* Specify a path to load from L2 cache including all its properties.
319+
*
320+
* <pre>{@code
321+
*
322+
* List<Order> orders =
323+
* new QOrder()
324+
* // eager fetch the customer using L2 cache
325+
* .fetchCache("customer")
326+
* .findList();
327+
*
328+
* }</pre>
329+
*
330+
* @param path the property path to load from L2 cache.
331+
*/
332+
public R fetchCache(String path) {
333+
query.fetchCache(path);
334+
return root;
335+
}
336+
317337
/**
318338
* Specify a path and properties to load using a "query join".
319339
*
@@ -327,13 +347,35 @@ public R fetchQuery(String path) {
327347
*
328348
* }</pre>
329349
*
330-
* @param path the property path of an associated (OneToOne, OneToMany, ManyToOne or ManyToMany) bean.
350+
* @param path the property path of an associated (OneToOne, OneToMany, ManyToOne or ManyToMany) bean.
351+
* @param properties the properties to load for this path.
331352
*/
332353
public R fetchQuery(String path, String properties) {
333354
query.fetchQuery(path, properties);
334355
return root;
335356
}
336357

358+
/**
359+
* Specify a path and properties to load from L2 cache.
360+
*
361+
* <pre>{@code
362+
*
363+
* List<Order> orders =
364+
* new QOrder()
365+
* // eager fetch the customer using L2 cache
366+
* .fetchCache("customer", "name,status")
367+
* .findList();
368+
*
369+
* }</pre>
370+
*
371+
* @param path the property path to load from L2 cache.
372+
* @param properties the properties to load for this path.
373+
*/
374+
public R fetchCache(String path, String properties) {
375+
query.fetchCache(path, properties);
376+
return root;
377+
}
378+
337379
/**
338380
* Specify a path to <em>fetch</em> with its specific properties to include
339381
* (aka partial object).

src/test/java/org/example/domain/Customer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.example.domain;
22

3+
import io.ebean.annotation.Cache;
34
import io.ebean.annotation.EnumValue;
45
import io.ebean.types.Inet;
56
import org.example.domain.finder.CustomerFinder;
@@ -17,6 +18,7 @@
1718
/**
1819
* Customer entity bean.
1920
*/
21+
@Cache
2022
@Entity
2123
@Table(name = "be_customer")
2224
public class Customer extends BaseModel {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.querytest;
2+
3+
import io.ebean.FetchGroup;
4+
import org.example.domain.Order;
5+
import org.example.domain.query.QCustomer;
6+
import org.example.domain.query.QOrder;
7+
import org.junit.Test;
8+
9+
public class QOrderTest {
10+
11+
private static final QCustomer cu = QCustomer.alias();
12+
13+
private static final QOrder or = QOrder.alias();
14+
15+
private static final FetchGroup<Order> fg = QOrder.forFetchGroup()
16+
.select(or.status, or.shipDate)
17+
.customer.fetchCache(cu.name, cu.status, cu.registered, cu.comments)
18+
.buildFetchGroup();
19+
20+
@Test
21+
public void fetchCache() {
22+
23+
24+
new QOrder()
25+
.status.eq(Order.Status.NEW)
26+
.customer.fetchCache(cu.name, cu.registered)
27+
.findList();
28+
29+
new QOrder()
30+
.status.eq(Order.Status.NEW)
31+
.customer.fetchCache()
32+
.findList();
33+
}
34+
35+
@Test
36+
public void viaFetchGraph() {
37+
38+
new QOrder()
39+
.status.eq(Order.Status.NEW)
40+
.select(fg)
41+
.findList();
42+
}
43+
44+
}

0 commit comments

Comments
 (0)