Fix for #209 - refresh() ... does not refresh OneToMany or ManyToMany properties

This commit is contained in:
rbygrave committed 2014-11-25 23:40:21 +13:00
1 parent d5d6ac675b
commit 6a6cff2236
27 files changed
+618 -727

No files matched your search

@@ -1,29 +1,8 @@
package com.avaje.ebeaninternal.server.query;
import java.lang.ref.WeakReference;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.persistence.PersistenceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.avaje.ebean.QueryIterator;
import com.avaje.ebean.bean.BeanCollection;
import com.avaje.ebean.bean.BeanCollectionAdd;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebean.bean.EntityBeanIntercept;
import com.avaje.ebean.bean.NodeUsageCollector;
import com.avaje.ebean.bean.NodeUsageListener;
import com.avaje.ebean.bean.ObjectGraphNode;
import com.avaje.ebean.bean.PersistenceContext;
import com.avaje.ebean.bean.*;
import com.avaje.ebean.config.GlobalProperties;
import com.avaje.ebeaninternal.api.LoadContext;
import com.avaje.ebeaninternal.api.SpiExpressionList;
import com.avaje.ebeaninternal.api.SpiQuery;
import com.avaje.ebeaninternal.api.SpiQuery.Mode;
@@ -32,16 +11,23 @@ import com.avaje.ebeaninternal.server.autofetch.AutoFetchManager;
import com.avaje.ebeaninternal.server.core.Message;
import com.avaje.ebeaninternal.server.core.OrmQueryRequest;
import com.avaje.ebeaninternal.server.core.SpiOrmQueryRequest;
import com.avaje.ebeaninternal.server.deploy.BeanCollectionHelp;
import com.avaje.ebeaninternal.server.deploy.BeanCollectionHelpFactory;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocMany;
import com.avaje.ebeaninternal.server.deploy.DbReadContext;
import com.avaje.ebeaninternal.server.deploy.*;
import com.avaje.ebeaninternal.server.el.ElPropertyValue;
import com.avaje.ebeaninternal.server.lib.util.StringHelper;
import com.avaje.ebeaninternal.server.querydefn.OrmQueryProperties;
import com.avaje.ebeaninternal.server.type.DataBind;
import com.avaje.ebeaninternal.server.type.DataReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.persistence.PersistenceException;
import java.lang.ref.WeakReference;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* An object that represents a SqlSelect statement.
@@ -82,7 +68,7 @@ public class CQuery<T> implements DbReadContext, CancelableQuery {
/**
* Flag set when 'master' bean changed.
*/
private boolean loadedBeanChanged;
private boolean loadedBeanChanged;
/**
* The 'master' bean just loaded.
@@ -220,6 +206,9 @@ public class CQuery<T> implements DbReadContext, CancelableQuery {
private long startNano;
private long executionTimeMicros;
private BeanCollectionAdd currentDetailAdd;
/**
* Create the Sql select based on the request.
*/
@@ -253,7 +242,7 @@ public class CQuery<T> implements DbReadContext, CancelableQuery {
// get filter to put on the collection for reuse with refresh
String manyPropertyName = sqlTree.getManyPropertyName();
OrmQueryProperties chunk = query.getDetail().getChunk(manyPropertyName, false);
this.filterMany = chunk.getFilterMany();
this.filterMany = (chunk == null) ? null : chunk.getFilterMany();
} else {
this.filterMany = null;
}
@@ -266,7 +255,7 @@ public class CQuery<T> implements DbReadContext, CancelableQuery {
this.predicates = predicates;
this.maxRowsLimit = query.getMaxRows() > 0 ? query.getMaxRows() : GLOBAL_ROW_LIMIT;
this.help = createHelp(request);
this.collection = (BeanCollection<T>) (help != null ? help.createEmpty(false) : null);
this.collection = (help != null ? help.createEmptyNoParent() : null);
}
private BeanCollectionHelp<T> createHelp(OrmQueryRequest<T> request) {
@@ -306,10 +295,6 @@ public class CQuery<T> implements DbReadContext, CancelableQuery {
return predicates;
}
public LoadContext getGraphContext() {
return request.getGraphContext();
}
public SpiOrmQueryRequest<?> getQueryRequest() {
return request;
}
@@ -470,12 +455,10 @@ public class CQuery<T> implements DbReadContext, CancelableQuery {
public EntityBean getLoadedBean() {
if (manyIncluded) {
if (prevDetailCollection instanceof BeanCollection<?>) {
((BeanCollection<?>) prevDetailCollection).setModifyListening(manyProperty
.getModifyListenMode());
((BeanCollection<?>) prevDetailCollection).setModifyListening(manyProperty.getModifyListenMode());
} else if (currentDetailCollection instanceof BeanCollection<?>) {
((BeanCollection<?>) currentDetailCollection).setModifyListening(manyProperty
.getModifyListenMode());
((BeanCollection<?>) currentDetailCollection).setModifyListening(manyProperty.getModifyListenMode());
}
}
@@ -581,8 +564,6 @@ public class CQuery<T> implements DbReadContext, CancelableQuery {
return false;
}
private BeanCollectionAdd currentDetailAdd;
private void createNewDetailCollection() {
prevDetailCollection = currentDetailCollection;
if (queryMode.equals(Mode.LAZYLOAD_MANY)) {
@@ -590,7 +571,7 @@ public class CQuery<T> implements DbReadContext, CancelableQuery {
currentDetailCollection = manyPropertyEl.elGetValue(loadedBean);
} else {
// create a new collection to populate and assign to the bean
currentDetailCollection = manyProperty.createEmpty(false);
currentDetailCollection = manyProperty.createEmpty(loadedBean);
manyPropertyEl.elSetValue(loadedBean, currentDetailCollection, false);
}
@@ -719,14 +700,6 @@ public class CQuery<T> implements DbReadContext, CancelableQuery {
return sqlTree.getSummary();
}
/**
* Return the SqlSelectChain. This is the flattened structure that represents
* this query.
*/
public SqlTree getSqlTree() {
return sqlTree;
}
public String getBindLog() {
return bindLog;
}
@@ -13,40 +13,71 @@ import com.avaje.ebeaninternal.server.el.ElPropertyValue;
*/
public class SqlTree {
private SqlTreeNode rootNode;
private final SqlTreeNode rootNode;
/**
* Property if resultSet contains master and detail rows.
*/
private BeanPropertyAssocMany<?> manyProperty;
private String manyPropertyName;
private ElPropertyValue manyPropEl;
private final BeanPropertyAssocMany<?> manyProperty;
private Set<String> includes;
private final String manyPropertyName;
private final ElPropertyValue manyPropEl;
private final Set<String> includes;
/**
* Summary of the select being generated.
*/
private String summary;
private final String summary;
private String selectSql;
private final String selectSql;
private String fromSql;
private final String fromSql;
/**
* Encrypted Properties require additional binding.
*/
private BeanProperty[] encryptedProps;
private final BeanProperty[] encryptedProps;
/**
* Where clause for inheritance.
*/
private String inheritanceWhereSql;
private final String inheritanceWhereSql;
/**
* Create the SqlSelectClause.
*/
public SqlTree() {
public SqlTree(String summary, SqlTreeNode rootNode, String selectSql, String fromSql, String inheritanceWhereSql,
BeanProperty[] encryptedProps, BeanPropertyAssocMany<?> manyProperty, String manyPropertyName,
ElPropertyValue manyPropEl, Set<String> includes) {
this.summary = summary;
this.rootNode = rootNode;
this.selectSql = selectSql;
this.fromSql = fromSql;
this.inheritanceWhereSql = inheritanceWhereSql;
this.encryptedProps = encryptedProps;
this.manyProperty = manyProperty;
this.manyPropertyName = manyPropertyName;
this.manyPropEl = manyPropEl;
this.includes = includes;
}
/**
* Construct for RawSql.
*/
public SqlTree(String summary, SqlTreeNode rootNode) {
this.summary = summary;
this.rootNode = rootNode;
this.selectSql = null;
this.fromSql = null;
this.inheritanceWhereSql = null;
this.encryptedProps = null;
this.manyProperty = null;
this.manyPropertyName = null;
this.manyPropEl = null;
this.includes = null;
}
public List<String> buildSelectExpressionChain() {
@@ -62,23 +93,6 @@ public class SqlTree {
return includes;
}
/**
* Set the association includes (Ones and Many's).
*/
public void setIncludes(Set<String> includes) {
this.includes = includes;
}
/**
* Set the manyProperty used for this query.
*/
public void setManyProperty(BeanPropertyAssocMany<?> manyProperty, String manyPropertyName,
ElPropertyValue manyPropEl) {
this.manyProperty = manyProperty;
this.manyPropertyName = manyPropertyName;
this.manyPropEl = manyPropEl;
}
/**
* Return the String for the actual SQL.
*/
@@ -86,21 +100,10 @@ public class SqlTree {
return selectSql;
}
/**
* Set the select sql clause.
*/
public void setSelectSql(String selectSql) {
this.selectSql = selectSql;
}
public String getFromSql() {
return fromSql;
}
public void setFromSql(String fromSql) {
this.fromSql = fromSql;
}
/**
* Return the where clause for inheritance.
*/
@@ -108,20 +111,6 @@ public class SqlTree {
return inheritanceWhereSql;
}
/**
* Set where clause(s) for inheritance.
*/
public void setInheritanceWhereSql(String whereSql) {
this.inheritanceWhereSql = whereSql;
}
/**
* Set the summary description of the query.
*/
public void setSummary(String summary) {
this.summary = summary;
}
/**
* Return a summary of the select clause.
*/
@@ -133,10 +122,6 @@ public class SqlTree {
return rootNode;
}
public void setRootNode(SqlTreeNode rootNode) {
this.rootNode = rootNode;
}
/**
* Return the property that is associated with the many. There can only be one
* per SqlSelect. This can be null.
@@ -164,7 +149,4 @@ public class SqlTree {
return encryptedProps;
}
public void setEncryptedProps(BeanProperty[] encryptedProps) {
this.encryptedProps = encryptedProps;
}
}
@@ -66,6 +66,8 @@ public class SqlTreeBuilder {
private final boolean rawSql;
private SqlTreeNode rootNode;
/**
* Construct for RawSql query.
*/
@@ -111,34 +113,33 @@ public class SqlTreeBuilder {
*/
public SqlTree build() {
SqlTree sqlTree = new SqlTree();
summary.append(desc.getName());
// build the appropriate chain of SelectAdapter's
buildRoot(desc, sqlTree);
buildRoot(desc);
// build the actual String
SqlTreeNode rootNode = sqlTree.getRootNode();
String selectSql = null;
String fromSql = null;
String inheritanceWhereSql = null;
BeanProperty[] encryptedProps = null;
if (!rawSql) {
sqlTree.setSelectSql(buildSelectClause(rootNode));
sqlTree.setFromSql(buildFromClause(rootNode));
sqlTree.setInheritanceWhereSql(buildWhereClause(rootNode));
sqlTree.setEncryptedProps(ctx.getEncryptedProps());
selectSql = buildSelectClause();
fromSql = buildFromClause();
inheritanceWhereSql = buildWhereClause();
encryptedProps = ctx.getEncryptedProps();
}
sqlTree.setIncludes(queryDetail.getIncludes());
sqlTree.setSummary(summary.toString());
ElPropertyValue manyPropEl = null;
if (manyPropertyName != null) {
ElPropertyValue manyPropEl = desc.getElGetValue(manyPropertyName);
sqlTree.setManyProperty(manyProperty, manyPropertyName, manyPropEl);
manyPropEl = desc.getElGetValue(manyPropertyName);
}
return sqlTree;
return new SqlTree(summary.toString(), rootNode, selectSql, fromSql, inheritanceWhereSql, encryptedProps,
manyProperty, manyPropertyName, manyPropEl, queryDetail.getIncludes());
}
private String buildSelectClause(SqlTreeNode rootNode) {
private String buildSelectClause() {
if (rawSql) {
return "Not Used";
@@ -155,7 +156,7 @@ public class SqlTreeBuilder {
return selectSql;
}
private String buildWhereClause(SqlTreeNode rootNode) {
private String buildWhereClause() {
if (rawSql) {
return "Not Used";
@@ -164,7 +165,7 @@ public class SqlTreeBuilder {
return ctx.getContent();
}
private String buildFromClause(SqlTreeNode rootNode) {
private String buildFromClause() {
if (rawSql) {
return "Not Used";
@@ -173,10 +174,9 @@ public class SqlTreeBuilder {
return ctx.getContent();
}
private void buildRoot(BeanDescriptor<?> desc, SqlTree sqlTree) {
private void buildRoot(BeanDescriptor<?> desc) {
SqlTreeNode selectRoot = buildSelectChain(null, null, desc, null);
sqlTree.setRootNode(selectRoot);
rootNode = buildSelectChain(null, null, desc, null);
if (!rawSql) {
alias.addJoin(queryDetail.getIncludes(), desc);