Fix for #57 - Embedded Entities with Autofetch Exception, plus some additional tests

This commit is contained in:
Rob Bygrave
2014-05-14 23:13:14 +12:00
parent 9f916998c9
commit 44ee64cde5
8 changed files with 331 additions and 12 deletions
@@ -451,6 +451,9 @@ public final class EntityBeanIntercept implements Serializable {
return -1;
}
/**
* Return the property name for the given property.
*/
public String getProperty(int propertyIndex) {
if (propertyIndex == -1) {
return null;
@@ -458,18 +461,38 @@ public final class EntityBeanIntercept implements Serializable {
return owner._ebean_getPropertyName(propertyIndex);
}
/**
* Return the number of properties.s
*/
public int getPropertyLength() {
return owner._ebean_getPropertyNames().length;
}
/**
* Set the property to be treated as unloaded. Used for properties initialised in default
* constructor.
*/
public void setPropertyUnloaded(int propertyIndex) {
loadedProps[propertyIndex] = false;
}
/**
* Set the property to be loaded.
*/
public void setLoadedProperty(int propertyIndex) {
loadedProps[propertyIndex] = true;
}
/**
* Return true if the property is loaded.
*/
public boolean isLoadedProperty(int propertyIndex) {
return loadedProps[propertyIndex];
}
/**
* Return true if the property is considered changed.
*/
public boolean isChangedProperty(int propertyIndex) {
return (changedProps != null && changedProps[propertyIndex]);
}
@@ -210,6 +210,11 @@ public class BeanDescriptor<T> implements MetaBeanInfo {
private final int versionPropertyIndex;
/**
* Properties that are initialised in the constructor need to be 'unloaded' to support partial object queries.
*/
private final int[] unloadProperties;
/**
* Properties local to this type (not from a super type).
*/
@@ -430,14 +435,46 @@ public class BeanDescriptor<T> implements MetaBeanInfo {
if (Modifier.isAbstract(beanType.getModifiers())) {
this.idPropertyIndex = -1;
this.versionPropertyIndex = -1;
this.unloadProperties = new int[0];
} else {
EntityBeanIntercept ebi = prototypeEntityBean._ebean_getIntercept();
this.idPropertyIndex = (idProperty == null) ? -1 : ebi.findProperty(idProperty.getName());
this.versionPropertyIndex = (versionProperty == null) ? -1 : ebi.findProperty(versionProperty.getName());
this.unloadProperties = derivePropertiesToUnload(prototypeEntityBean);
}
}
/**
* Derive an array of property positions for properties that are initialised in the constructor.
* These properties need to be unloaded when populating beans for queries.
*/
private int[] derivePropertiesToUnload(EntityBean prototypeEntityBean) {
boolean[] loaded = prototypeEntityBean._ebean_getIntercept().getLoaded();
int[] props = new int[loaded.length];
int pos = 0;
// collect the positions of the properties initialised in the default constructor.
for (int i = 0; i < loaded.length; i++) {
if (loaded[i]) {
props[pos++] = i;
}
}
if (pos == 0) {
// nothing set in the constructor
return new int[0];
}
// populate a smaller/minimal array
int[] unload = new int[pos];
for (int i = 0; i < pos; i++) {
unload[i] = props[i];
}
return unload;
}
/**
* Create an entity bean that is used as a prototype/factory to create new instances.
*/
@@ -1142,7 +1179,17 @@ public class BeanDescriptor<T> implements MetaBeanInfo {
*/
public EntityBean createEntityBean() {
try {
return (EntityBean)prototypeEntityBean._ebean_newInstance();
EntityBean bean = (EntityBean)prototypeEntityBean._ebean_newInstance();
if (unloadProperties.length > 0) {
// 'unload' any properties initialised in the default constructor
EntityBeanIntercept ebi = bean._ebean_getIntercept();
for (int i = 0; i < unloadProperties.length; i++) {
ebi.setPropertyUnloaded(unloadProperties[i]);
}
}
return bean;
} catch (Exception ex) {
throw new PersistenceException(ex);
}
@@ -65,8 +65,8 @@ public class SqlTreeAlias {
for (String propJoin : propJoins) {
ElPropertyDeploy elProp = desc.getElPropertyDeploy(propJoin);
if (elProp != null && elProp.getBeanProperty().isEmbedded()) {
String[] split = SplitName.split(propJoin);
addPropertyJoin(split[0], joinProps);
//String[] split = SplitName.split(propJoin);
//addPropertyJoin(split[0], joinProps);
addEmbeddedPropertyJoin(propJoin);
} else {