A query on inherited beans can be restricted to the subtype of one of its children (#1602)

ENH: A query on inherited beans can be restricted to the subtype of one of its children
This commit is contained in:
Roland Praml
2019-01-09 19:15:04 +13:00
committed by Rob Bygrave
parent cd8c12a9a8
commit 31c6d16e73
3 changed files with 81 additions and 1 deletions
+10
View File
@@ -1610,6 +1610,16 @@ public interface Query<T> {
*/
Class<T> getBeanType();
/**
* Sets the inherit type. Must be a subtype of getBeanType
*/
Query<T> setInheritType(Class<? extends T> type);
/**
* Returns the inherit type. This is normally the same as getBeanType() returns as long as no other type is set.
*/
Class<? extends T> getInheritType();
/**
* Return the type of query being executed.
*/
@@ -47,6 +47,7 @@ import io.ebeaninternal.server.autotune.ProfilingListener;
import io.ebeaninternal.server.core.SpiOrmQueryRequest;
import io.ebeaninternal.server.deploy.BeanDescriptor;
import io.ebeaninternal.server.deploy.BeanPropertyAssocMany;
import io.ebeaninternal.server.deploy.InheritInfo;
import io.ebeaninternal.server.deploy.TableJoin;
import io.ebeaninternal.server.expression.DefaultExpressionList;
import io.ebeaninternal.server.expression.SimpleExpression;
@@ -79,7 +80,9 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
private final Class<T> beanType;
private final BeanDescriptor<T> beanDescriptor;
private final BeanDescriptor<T> rootBeanDescriptor;
private BeanDescriptor<T> beanDescriptor;
private final SpiEbeanServer server;
@@ -276,6 +279,7 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
public DefaultOrmQuery(BeanDescriptor<T> desc, SpiEbeanServer server, ExpressionFactory expressionFactory) {
this.beanDescriptor = desc;
this.rootBeanDescriptor = desc;
this.beanType = desc.getBeanType();
this.server = server;
this.orderById = server.getServerConfig().isDefaultOrderById();
@@ -1641,6 +1645,25 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
return beanType;
}
@Override
public Class<? extends T> getInheritType() {
return beanDescriptor.getBeanType();
}
@Override
public Query<T> setInheritType(Class<? extends T> type) {
if (type == beanType) {
return this;
}
InheritInfo inheritInfo = rootBeanDescriptor.getInheritInfo();
inheritInfo = inheritInfo == null ? null : inheritInfo.readType(type);
if (inheritInfo == null) {
throw new IllegalArgumentException("Given type " + type + " is not a subtype of " + beanType);
}
beanDescriptor = (BeanDescriptor<T>) rootBeanDescriptor.getBeanDescriptor(type);
return this;
}
@Override
public String toString() {
return "Query [" + whereExpressions + "]";