Merge in v4 code changes

This commit is contained in:
Rob Bygrave committed 2014-04-03 03:47:55 +13:00
commit 96e4b4640f
171 files changed
+2747 -3848

No files matched your search

@@ -4,6 +4,7 @@ import java.util.Collection;
import java.util.Map;
import com.avaje.ebean.bean.BeanCollection;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebeaninternal.api.SpiQuery;
import com.avaje.ebeaninternal.server.core.OrmQueryRequest;
import com.avaje.ebeaninternal.server.core.RelationalQueryRequest;
@@ -149,7 +150,7 @@ public final class BeanCollectionWrapper {
/**
* Add the bean to the collection held in this wrapper.
*/
public void add(Object bean) {
public void add(EntityBean bean) {
add(bean, beanCollection);
}
@@ -162,7 +163,7 @@ public final class BeanCollectionWrapper {
* the collection or map to add the bean to
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public void add(Object bean, Object collection) {
public void add(EntityBean bean, Object collection) {
if (bean == null) {
return;
}
@@ -12,7 +12,6 @@ import javax.persistence.PersistenceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.avaje.ebean.QueryIterator;
import com.avaje.ebean.QueryListener;
import com.avaje.ebean.bean.BeanCollection;
@@ -84,11 +83,12 @@ public class CQuery<T> implements DbReadContext, CancelableQuery {
/**
* Flag set when 'master' bean changed.
*/
private boolean loadedBeanChanged;
private boolean loadedBeanChanged;
/**
* The 'master' bean just loaded.
*/
private Object loadedBean;
private EntityBean loadedBean;
private final BeanPropertyAssocMany<?> lazyLoadManyProperty;
@@ -99,12 +99,12 @@ public class CQuery<T> implements DbReadContext, CancelableQuery {
/**
* Holds the previous loaded bean.
*/
private Object prevLoadedBean;
private EntityBean prevLoadedBean;
/**
* The detail bean just loaded.
*/
private Object loadedManyBean;
private EntityBean loadedManyBean;
/**
* The previous 'detail' collection remembered so that for manyToMany we can
@@ -213,6 +213,7 @@ public class CQuery<T> implements DbReadContext, CancelableQuery {
private final CQueryPlan queryPlan;
private final Mode queryMode;
private final boolean autoFetchProfiling;
@@ -223,6 +224,7 @@ public class CQuery<T> implements DbReadContext, CancelableQuery {
private final WeakReference<NodeUsageListener> autoFetchManagerRef;
private final Boolean readOnly;
private final SpiExpressionList<?> filterMany;
@@ -230,7 +232,6 @@ public class CQuery<T> implements DbReadContext, CancelableQuery {
private long startNano;
private long executionTimeMicros;
/**
* Create the Sql select based on the request.
*/
@@ -445,7 +446,7 @@ public class CQuery<T> implements DbReadContext, CancelableQuery {
return persistenceContext;
}
public void setLoadedBean(Object bean, Object id, Object lazyLoadParentId) {
public void setLoadedBean(EntityBean bean, Object id, Object lazyLoadParentId) {
if (id != null && id.equals(loadedBeanId)) {
// master/detail loading with master bean
// unchanged. NB Using id to avoid any issue
@@ -475,15 +476,14 @@ public class CQuery<T> implements DbReadContext, CancelableQuery {
}
}
public void setLoadedManyBean(Object manyValue) {
public void setLoadedManyBean(EntityBean manyValue) {
this.loadedManyBean = manyValue;
}
/**
* Return the last read bean.
*/
@SuppressWarnings("unchecked")
public T getLoadedBean() {
public EntityBean getLoadedBean() {
if (manyIncluded) {
if (prevDetailCollection instanceof BeanCollection<?>) {
((BeanCollection<?>) prevDetailCollection).setModifyListening(manyProperty
@@ -496,9 +496,9 @@ public class CQuery<T> implements DbReadContext, CancelableQuery {
}
if (prevLoadedBean != null) {
return (T) prevLoadedBean;
return prevLoadedBean;
} else {
return (T) loadedBean;
return loadedBean;
}
}
@@ -684,10 +684,11 @@ public class CQuery<T> implements DbReadContext, CancelableQuery {
}
}
@SuppressWarnings("unchecked")
private void readTheRows(boolean inForeground) throws SQLException {
while (hasNextBean(inForeground)) {
if (queryListener != null) {
queryListener.process(getLoadedBean());
queryListener.process((T)getLoadedBean());
} else {
// add to the list/set/map
@@ -697,7 +698,7 @@ public class CQuery<T> implements DbReadContext, CancelableQuery {
}
protected boolean hasNextBean(boolean inForeground) throws SQLException {
if (!readBeanInternal(inForeground)) {
return false;
@@ -10,6 +10,7 @@ import com.avaje.ebean.BackgroundExecutor;
import com.avaje.ebean.QueryIterator;
import com.avaje.ebean.bean.BeanCollection;
import com.avaje.ebean.bean.BeanCollectionTouched;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebean.bean.ObjectGraphNode;
import com.avaje.ebean.config.dbplatform.DatabasePlatform;
import com.avaje.ebeaninternal.api.BeanIdList;
@@ -226,7 +227,7 @@ public class CQueryEngine {
*/
public <T> T find(OrmQueryRequest<T> request) {
T bean = null;
EntityBean bean = null;
CQuery<T> cquery = queryBuilder.buildQuery(request);
@@ -247,7 +248,7 @@ public class CQueryEngine {
request.executeSecondaryQueries(defaultSecondaryQueryBatchSize);
return bean;
return (T)bean;
} catch (SQLException e) {
throw cquery.createPersistenceException(e);
@@ -12,6 +12,7 @@ import java.util.concurrent.FutureTask;
import com.avaje.ebean.BackgroundExecutor;
import com.avaje.ebean.bean.BeanCollection;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebean.bean.EntityBeanIntercept;
import com.avaje.ebean.bean.PersistenceContext;
import com.avaje.ebeaninternal.api.BeanIdList;
@@ -299,11 +300,11 @@ public class CQueryFetchIds {
// no-op
}
public void setLoadedBean(Object loadedBean, Object id, Object lazyLoadParentId) {
public void setLoadedBean(EntityBean loadedBean, Object id, Object lazyLoadParentId) {
// no-op
}
public void setLoadedManyBean(Object loadedBean) {
public void setLoadedManyBean(EntityBean loadedBean) {
// no-op
}
@@ -4,6 +4,7 @@ import java.util.Collection;
import com.avaje.ebean.QueryIterator;
import com.avaje.ebean.bean.BeanCollection;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebean.event.BeanFinder;
import com.avaje.ebeaninternal.api.BeanIdList;
import com.avaje.ebeaninternal.api.SpiQuery;
@@ -82,7 +83,7 @@ public class DefaultOrmQueryEngine implements OrmQueryEngine {
BeanDescriptor<T> descriptor = request.getBeanDescriptor();
Collection<T> c = result.getActualDetails();
for (T bean : c) {
descriptor.cachePutBeanData(bean);
descriptor.cachePutBeanData((EntityBean)bean);
}
}
@@ -119,7 +120,7 @@ public class DefaultOrmQueryEngine implements OrmQueryEngine {
}
if (result != null && request.isUseBeanCache()){
request.getBeanDescriptor().cachePutBeanData(result);
request.getBeanDescriptor().cachePutBeanData((EntityBean)result);
}
return result;
@@ -1,7 +1,6 @@
package com.avaje.ebeaninternal.server.query;
import java.sql.SQLException;
import java.util.Set;
import javax.persistence.PersistenceException;
@@ -22,97 +21,73 @@ import com.avaje.ebeaninternal.server.deploy.DbReadContext;
public class SqlBeanLoad {
private final DbReadContext ctx;
private final Object bean;
private final EntityBean bean;
private final EntityBeanIntercept ebi;
private final Class<?> type;
private final Object originalOldValues;
private final boolean isLazyLoad;
private final boolean lazyLoading;
private final boolean refreshLoading;
private final boolean rawSql;
// set of properties to exclude from the refresh because it is
// not a refresh but rather a lazyLoading event.
private final Set<String> excludes;
private final boolean setOriginalOldValues;
private final boolean rawSql;
public SqlBeanLoad(DbReadContext ctx, Class<?> type, Object bean, Mode queryMode) {
this.ctx = ctx;
this.rawSql = ctx.isRawSql();
this.type = type;
this.isLazyLoad = queryMode.equals(Mode.LAZYLOAD_BEAN);
this.bean = bean;
if (bean instanceof EntityBean) {
EntityBeanIntercept ebi = ((EntityBean) bean)._ebean_getIntercept();
public SqlBeanLoad(DbReadContext ctx, Class<?> type, EntityBean bean, Mode queryMode) {
this.excludes = isLazyLoad ? ebi.getLoadedProps() : null;
if (excludes != null) {
// lazy loading a "Partial Object"... which already
// contains some properties and perhaps some oldValues
// and these will need to be maintained...
originalOldValues = ebi.getOldValues();
} else {
originalOldValues = null;
}
this.setOriginalOldValues = originalOldValues != null;
} else {
this.excludes = null;
this.originalOldValues = null;
this.setOriginalOldValues = false;
}
}
this.ctx = ctx;
this.rawSql = ctx.isRawSql();
this.type = type;
this.lazyLoading = queryMode.equals(Mode.LAZYLOAD_BEAN);
this.refreshLoading = queryMode.equals(Mode.REFRESH_BEAN);
this.bean = bean;
this.ebi = bean == null ? null : bean._ebean_getIntercept();
}
/**
* Return true if this is a lazy loading.
*/
public boolean isLazyLoad() {
return isLazyLoad;
/**
* Return true if this is a lazy loading.
*/
public boolean isLazyLoad() {
return lazyLoading;
}
/**
* Increment the resultSet index 1.
*/
public void loadIgnore(int increment) {
ctx.getDataReader().incrementPos(increment);
}
public Object load(BeanProperty prop) throws SQLException {
if (!rawSql && prop.isTransient()) {
return null;
}
/**
* Increment the resultSet index 1.
*/
public void loadIgnore(int increment) {
ctx.getDataReader().incrementPos(increment);
}
public Object load(BeanProperty prop) throws SQLException {
if (!rawSql && prop.isTransient()){
return null;
}
if ((bean == null)
|| (excludes != null && excludes.contains(prop.getName()))
|| (type != null && !prop.isAssignableFrom(type))){
if ((bean == null)
|| (lazyLoading && ebi.isLoadedProperty(prop.getPropertyIndex()))
|| (type != null && !prop.isAssignableFrom(type))) {
// ignore this property
// ... null: bean already in persistence context
// ... excludes: partial bean that is lazy loading
// ... type: inheritance and not assignable to this instance
prop.loadIgnore(ctx);
return null;
}
try {
Object dbVal = prop.read(ctx);
if (isLazyLoad){
prop.setValue(bean, dbVal);
} else {
prop.setValueIntercept(bean, dbVal);
}
if (setOriginalOldValues){
// maintain original oldValues for partially loaded bean
prop.setValue(originalOldValues, dbVal);
}
return dbVal;
} catch (Exception e) {
String msg = "Error loading on " + prop.getFullBeanName();
throw new PersistenceException(msg, e);
}
}
// ignore this property
// ... null: bean already in persistence context
// ... lazyLoading: partial bean that is lazy loading
// ... type: inheritance and not assignable to this instance
prop.loadIgnore(ctx);
return null;
}
try {
Object dbVal = prop.read(ctx);
if (!refreshLoading) {
prop.setValue(bean, dbVal);
} else {
prop.setValueIntercept(bean, dbVal);
}
return dbVal;
} catch (Exception e) {
String msg = "Error loading on " + prop.getFullBeanName();
throw new PersistenceException(msg, e);
}
}
public void loadAssocMany(BeanPropertyAssocMany<?> prop) {
@@ -376,9 +376,6 @@ public class SqlTreeBuilder {
// add the embedded bean (and effectively
// all its properties)
selectProps.add(p);
// also make sure it is added to included properties
// to avoid unnecessary lazy loading
selectProps.getIncludedProperties().add(baseName);
} else {
String m = "property [" + p.getFullBeanName()
@@ -416,12 +413,10 @@ public class SqlTreeBuilder {
}
}
private SqlTreeProperties getBaseSelectPartial(BeanDescriptor<?> desc,
OrmQueryProperties queryProps) {
private SqlTreeProperties getBaseSelectPartial(BeanDescriptor<?> desc, OrmQueryProperties queryProps) {
SqlTreeProperties selectProps = new SqlTreeProperties();
SqlTreeProperties selectProps = new SqlTreeProperties(desc);
selectProps.setReadOnly(queryProps.isReadOnly());
selectProps.setIncludedProperties(queryProps.getAllIncludedProperties());
// add properties in the order in which they appear
// in the query. Gives predictable sql/properties for
@@ -448,7 +443,8 @@ public class SqlTreeBuilder {
return getBaseSelectPartial(desc, queryProps);
}
SqlTreeProperties selectProps = new SqlTreeProperties();
SqlTreeProperties selectProps = new SqlTreeProperties(desc);
selectProps.setAllProperties(true);
// normal simple properties of the bean
selectProps.add(desc.propertiesBaseScalar());
@@ -3,6 +3,7 @@ package com.avaje.ebeaninternal.server.query;
import java.sql.SQLException;
import java.util.List;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebeaninternal.server.deploy.DbReadContext;
import com.avaje.ebeaninternal.server.deploy.DbSqlContext;
@@ -42,6 +43,6 @@ public interface SqlTreeNode {
* </p>
*
*/
public void load(DbReadContext ctx, Object parentBean) throws SQLException;
public void load(DbReadContext ctx, EntityBean parentBean) throws SQLException;
}
@@ -45,17 +45,6 @@ public class SqlTreeNodeBean implements SqlTreeNode {
*/
final boolean partialObject;
/**
* The set of properties explicitly included in the query. We actually add the
* manyProp names to this as they are references/proxies we add via
* createListProxies().
*/
final Set<String> partialProps;
/**
* The hash of the partialProps (calculate once).
*/
int partialHash;
final BeanProperty[] properties;
@@ -79,9 +68,9 @@ public class SqlTreeNodeBean implements SqlTreeNode {
final String prefix;
Set<String> includedProps;
final Map<String, String> pathMap;
final BeanPropertyAssocMany<?> lazyLoadParent;
@@ -113,21 +102,11 @@ public class SqlTreeNodeBean implements SqlTreeNode {
this.tableJoins = props.getTableJoins();
this.partialObject = props.isPartialObject();
this.partialProps = props.getIncludedProperties();
this.partialHash = partialObject ? partialProps.hashCode() : 0;
this.readOnlyLeaf = props.isReadOnly();
this.properties = props.getProps();
if (partialObject) {
// merge the explicit partialProps with the implicitly added
// list proxies (that are added by createListProxies()) to get
// the full set of 'loaded' properties for this bean.
includedProps = LoadedPropertiesCache.get(partialHash, partialProps, desc);
} else {
includedProps = null;
}
if (myChildren == null) {
children = NO_CHILDREN;
@@ -159,7 +138,7 @@ public class SqlTreeNodeBean implements SqlTreeNode {
}
}
protected void postLoad(DbReadContext cquery, Object loadedBean, Object id, Object lazyLoadParentId) {
protected void postLoad(DbReadContext cquery, EntityBean loadedBean, Object id, Object lazyLoadParentId) {
}
public void buildSelectExpressionChain(List<String> selectChain) {
@@ -180,7 +159,7 @@ public class SqlTreeNodeBean implements SqlTreeNode {
/**
* read the properties from the resultSet.
*/
public void load(DbReadContext ctx, Object parentBean) throws SQLException {
public void load(DbReadContext ctx, EntityBean parentBean) throws SQLException {
Object lazyLoadParentId = null;
if (lazyLoadParent != null) {
@@ -188,12 +167,12 @@ public class SqlTreeNodeBean implements SqlTreeNode {
}
// bean already existing in the persistence context
Object contextBean = null;
EntityBean contextBean = null;
Class<?> localType;
BeanDescriptor<?> localDesc;
IdBinder localIdBinder;
Object localBean;
EntityBean localBean;
if (inheritInfo != null) {
InheritInfo localInfo = inheritInfo.readType(ctx);
@@ -232,7 +211,7 @@ public class SqlTreeNodeBean implements SqlTreeNode {
localBean = null;
} else {
// check the PersistenceContext to see if the bean already exists
contextBean = persistenceContext.putIfAbsent(id, localBean);
contextBean = (EntityBean)persistenceContext.putIfAbsent(id, localBean);
if (contextBean == null) {
// bean just added to the persistenceContext
contextBean = localBean;
@@ -241,10 +220,6 @@ public class SqlTreeNodeBean implements SqlTreeNode {
if (isLoadContextBeanNeeded(queryMode, contextBean)){
// refresh it anyway (lazy loading for example)
localBean = contextBean;
if (localBean instanceof EntityBean) {
// temporarily turn off interception during load
((EntityBean) localBean)._ebean_getIntercept().setIntercepting(false);
}
} else {
// ignore the DB data...
localBean = null;
@@ -305,12 +280,11 @@ public class SqlTreeNodeBean implements SqlTreeNode {
ctx.setCurrentPrefix(prefix, pathMap);
createListProxies(localDesc, ctx, localBean);
localDesc.postLoad(localBean, includedProps);
localDesc.postLoad(localBean, null);
if (localBean instanceof EntityBean) {
EntityBeanIntercept ebi = ((EntityBean) localBean)._ebean_getIntercept();
ebi.setPersistenceContext(persistenceContext);
ebi.setLoadedProps(includedProps);
if (Mode.LAZYLOAD_BEAN.equals(queryMode)) {
// Lazy Load does not reset the dirty state
ebi.setLoadedLazy();
@@ -321,6 +295,8 @@ public class SqlTreeNodeBean implements SqlTreeNode {
if (partialObject) {
ctx.register(null, ebi);
} else {
ebi.setFullyLoadedBean(true);
}
if (disableLazyLoad) {
@@ -355,7 +331,7 @@ public class SqlTreeNodeBean implements SqlTreeNode {
* Create lazy loading proxies for the Many's except for the one that is
* included in the actual query.
*/
private void createListProxies(BeanDescriptor<?> localDesc, DbReadContext ctx, Object localBean) {
private void createListProxies(BeanDescriptor<?> localDesc, DbReadContext ctx, EntityBean localBean) {
BeanPropertyAssocMany<?> fetchedMany = ctx.getManyProperty();
@@ -4,6 +4,7 @@ import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssoc;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocMany;
import com.avaje.ebeaninternal.server.deploy.DbReadContext;
@@ -120,7 +121,7 @@ public class SqlTreeNodeExtraJoin implements SqlTreeNode {
/**
* Does nothing.
*/
public void load(DbReadContext ctx, Object parentBean) throws SQLException {
public void load(DbReadContext ctx, EntityBean parentBean) throws SQLException {
}
}
@@ -14,7 +14,7 @@ public final class SqlTreeNodeManyRoot extends SqlTreeNodeBean {
}
@Override
protected void postLoad(DbReadContext cquery, Object loadedBean, Object id, Object lazyLoadParentId) {
protected void postLoad(DbReadContext cquery, EntityBean loadedBean, Object id, Object lazyLoadParentId) {
// put the localBean into the manyValue so that it
// is added to the collection/map
@@ -22,7 +22,7 @@ public final class SqlTreeNodeManyRoot extends SqlTreeNodeBean {
}
@Override
public void load(DbReadContext cquery, Object parentBean) throws SQLException {
public void load(DbReadContext cquery, EntityBean parentBean) throws SQLException {
// pass in null for parentBean because the localBean
// that is built is added to a collection rather than
// being set to the parentBean directly
@@ -4,6 +4,7 @@ import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssoc;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocMany;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocOne;
@@ -86,7 +87,7 @@ public class SqlTreeNodeManyWhereJoin implements SqlTreeNode {
// nothing to do here
}
public void load(DbReadContext ctx, Object parentBean) throws SQLException {
public void load(DbReadContext ctx, EntityBean parentBean) throws SQLException {
// nothing to do here
}
@@ -2,6 +2,7 @@ package com.avaje.ebeaninternal.server.query;
import java.util.List;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocMany;
import com.avaje.ebeaninternal.server.deploy.DbReadContext;
@@ -29,7 +30,7 @@ public final class SqlTreeNodeRoot extends SqlTreeNodeBean {
}
@Override
protected void postLoad(DbReadContext cquery, Object loadedBean, Object id, Object lazyLoadParentId) {
protected void postLoad(DbReadContext cquery, EntityBean loadedBean, Object id, Object lazyLoadParentId) {
// set the current bean with id...
cquery.setLoadedBean(loadedBean, id, lazyLoadParentId);
@@ -5,6 +5,7 @@ import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
import com.avaje.ebeaninternal.server.deploy.TableJoin;
@@ -13,10 +14,14 @@ import com.avaje.ebeaninternal.server.deploy.TableJoin;
*/
public class SqlTreeProperties {
/**
* The included Properties that will be used by EntityBeanIntercept
* to determine lazy loading on partial objects.
*/
private static final TableJoin[] EMPTY_TABLE_JOINS = new TableJoin[0];
private final BeanDescriptor<?> desc;
// /**
// * The included Properties that will be used by EntityBeanIntercept
// * to determine lazy loading on partial objects.
// */
Set<String> includedProps;
/**
@@ -29,7 +34,7 @@ public class SqlTreeProperties {
*/
boolean includeId = true;
TableJoin[] tableJoins = new TableJoin[0];
TableJoin[] tableJoins = EMPTY_TABLE_JOINS;
/**
* The bean properties in order.
@@ -40,9 +45,11 @@ public class SqlTreeProperties {
* Maintain a list of property names to detect embedded bean additions.
*/
LinkedHashSet<String> propNames = new LinkedHashSet<String>();
public SqlTreeProperties() {
private boolean allProperties;
public SqlTreeProperties(BeanDescriptor<?> desc) {
this.desc = desc;
}
public boolean containsProperty(String propName){
@@ -56,9 +63,8 @@ public class SqlTreeProperties {
}
public void add(BeanProperty prop) {
propsList.add(prop);
propNames.add(prop.getName());
propsList.add(prop);
propNames.add(prop.getName());
}
public BeanProperty[] getProps() {
@@ -74,15 +80,7 @@ public class SqlTreeProperties {
}
public boolean isPartialObject() {
return includedProps != null;
}
public Set<String> getIncludedProperties() {
return includedProps;
}
public void setIncludedProperties(Set<String> includedProps) {
this.includedProps = includedProps;
return !allProperties;
}
public boolean isReadOnly() {
@@ -101,4 +99,12 @@ public class SqlTreeProperties {
this.tableJoins = tableJoins;
}
public void setAllProperties(boolean allProperties) {
this.allProperties = allProperties;
}
public boolean isAllProperties() {
return allProperties;
}
}