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

Commit

Permalink
#66 - ENH: Support fetchCache()
Browse files Browse the repository at this point in the history
  • Loading branch information
rbygrave committed Aug 20, 2019
1 parent c2a3927 commit 1623f70
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 5 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
<dependency>
<groupId>io.ebean</groupId>
<artifactId>ebean</artifactId>
<version>11.43.1</version>
<version>11.43.2</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>io.ebean</groupId>
<artifactId>querybean-generator</artifactId>
<version>11.41.1</version>
<version>11.43.2</version>
<scope>provided</scope>
</dependency>

Expand Down
27 changes: 27 additions & 0 deletions src/main/java/io/ebean/typequery/TQAssocBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ public R fetchQuery() {
return _root;
}

/**
* Eagerly fetch this association using L2 bean cache.
* Cache misses are populated via fetchQuery().
*/
public R fetchCache() {
((TQRootBean) _root).query().fetchCache(_name);
return _root;
}

/**
* Use lazy loading for fetching this association.
*/
Expand All @@ -68,6 +77,15 @@ public R fetchQuery(String properties) {
return _root;
}

/**
* Eagerly fetch this association using L2 cache with the properties specified.
* Cache misses are populated via fetchQuery().
*/
public R fetchCache(String properties) {
((TQRootBean) _root).query().fetchCache(_name, properties);
return _root;
}

/**
* Deprecated in favor of fetch().
*
Expand Down Expand Up @@ -95,6 +113,15 @@ protected final R fetchQueryProperties(TQProperty<?>... props) {
return _root;
}

/**
* Eagerly fetch this association using L2 bean cache.
*/
@SafeVarargs
protected final R fetchCacheProperties(TQProperty<?>... props) {
((TQRootBean) _root).query().fetchCache(_name, properties(props));
return _root;
}

/**
* Eagerly fetch query this association fetching some of the properties.
*/
Expand Down
48 changes: 45 additions & 3 deletions src/main/java/io/ebean/typequery/TQRootBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public TQRootBean(Class<T> beanType, Database database) {
@SuppressWarnings("unchecked")
public TQRootBean(Query<T> query) {
this.query = query;
this.root = (R)this;
this.root = (R) this;
}

/**
Expand All @@ -157,7 +157,7 @@ public TQRootBean(boolean aliasDummy) {
* Return the fetch group.
*/
public FetchGroup<T> buildFetchGroup() {
return ((SpiFetchGroupQuery)query()).buildFetchGroup();
return ((SpiFetchGroupQuery) query()).buildFetchGroup();
}

/**
Expand Down Expand Up @@ -314,6 +314,26 @@ public R fetchQuery(String path) {
return root;
}

/**
* Specify a path to load from L2 cache including all its properties.
*
* <pre>{@code
*
* List<Order> orders =
* new QOrder()
* // eager fetch the customer using L2 cache
* .fetchCache("customer")
* .findList();
*
* }</pre>
*
* @param path the property path to load from L2 cache.
*/
public R fetchCache(String path) {
query.fetchCache(path);
return root;
}

/**
* Specify a path and properties to load using a "query join".
*
Expand All @@ -327,13 +347,35 @@ public R fetchQuery(String path) {
*
* }</pre>
*
* @param path the property path of an associated (OneToOne, OneToMany, ManyToOne or ManyToMany) bean.
* @param path the property path of an associated (OneToOne, OneToMany, ManyToOne or ManyToMany) bean.
* @param properties the properties to load for this path.
*/
public R fetchQuery(String path, String properties) {
query.fetchQuery(path, properties);
return root;
}

/**
* Specify a path and properties to load from L2 cache.
*
* <pre>{@code
*
* List<Order> orders =
* new QOrder()
* // eager fetch the customer using L2 cache
* .fetchCache("customer", "name,status")
* .findList();
*
* }</pre>
*
* @param path the property path to load from L2 cache.
* @param properties the properties to load for this path.
*/
public R fetchCache(String path, String properties) {
query.fetchCache(path, properties);
return root;
}

/**
* Specify a path to <em>fetch</em> with its specific properties to include
* (aka partial object).
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/org/example/domain/Customer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.example.domain;

import io.ebean.annotation.Cache;
import io.ebean.annotation.EnumValue;
import io.ebean.types.Inet;
import org.example.domain.finder.CustomerFinder;
Expand All @@ -17,6 +18,7 @@
/**
* Customer entity bean.
*/
@Cache
@Entity
@Table(name = "be_customer")
public class Customer extends BaseModel {
Expand Down
44 changes: 44 additions & 0 deletions src/test/java/org/querytest/QOrderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.querytest;

import io.ebean.FetchGroup;
import org.example.domain.Order;
import org.example.domain.query.QCustomer;
import org.example.domain.query.QOrder;
import org.junit.Test;

public class QOrderTest {

private static final QCustomer cu = QCustomer.alias();

private static final QOrder or = QOrder.alias();

private static final FetchGroup<Order> fg = QOrder.forFetchGroup()
.select(or.status, or.shipDate)
.customer.fetchCache(cu.name, cu.status, cu.registered, cu.comments)
.buildFetchGroup();

@Test
public void fetchCache() {


new QOrder()
.status.eq(Order.Status.NEW)
.customer.fetchCache(cu.name, cu.registered)
.findList();

new QOrder()
.status.eq(Order.Status.NEW)
.customer.fetchCache()
.findList();
}

@Test
public void viaFetchGraph() {

new QOrder()
.status.eq(Order.Status.NEW)
.select(fg)
.findList();
}

}

0 comments on commit 1623f70

Please sign in to comment.