Fix for #91 Stateless Update API Change, #90 Removed BeanState.setReference(), #89 Remove support for multiple @Id properties

This commit is contained in:
Rob Bygrave committed 2014-04-23 01:51:27 +12:00
1 parent 5e2d592c2a
commit 0d1c9e2883
66 files changed
+1045 -1768

No files matched your search

@@ -1,5 +1,6 @@
package com.avaje.ebeaninternal.server.deploy;
import java.lang.reflect.Modifier;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
@@ -217,12 +218,15 @@ public class BeanDescriptor<T> implements MetaBeanInfo {
/**
* Derived list of properties that make up the unique id.
*/
private final BeanProperty[] propertiesId;
private final BeanProperty idProperty;
private final int idPropertyIndex;
/**
* Derived list of properties that are used for version concurrency checking.
*/
private final BeanProperty versionProperty;
private final int versionPropertyIndex;
private final BeanProperty propertiesNaturalKey;
/**
@@ -281,14 +285,7 @@ public class BeanDescriptor<T> implements MetaBeanInfo {
/**
* All non transient properties excluding the id properties.
*/
final BeanProperty[] propertiesNonTransient;
/**
* Set when the Id property is a single non-embedded property. Can make life
* simpler for this case.
*/
private final BeanProperty propertySingleId;
private final BeanProperty[] propertiesNonTransient;
/**
* The bean class name or the table name for MapBeans.
@@ -403,15 +400,15 @@ public class BeanDescriptor<T> implements MetaBeanInfo {
// helper object used to derive lists of properties
DeployBeanPropertyLists listHelper = new DeployBeanPropertyLists(owner, this, deploy);
this.idProperty = listHelper.getId();
this.versionProperty = listHelper.getVersionProperty();
this.propMap = listHelper.getPropertyMap();
this.propMapByDbColumn = getReverseMap(propMap);
this.propertiesTransient = listHelper.getTransients();
this.propertiesNonTransient = listHelper.getNonTransients();
this.propertiesBaseScalar = listHelper.getBaseScalar();
this.propertiesBaseCompound = listHelper.getBaseCompound();
this.propertiesId = listHelper.getId();
this.propertiesNaturalKey = listHelper.getNaturalKey();
this.versionProperty = listHelper.getVersionProperty();
this.propertiesEmbedded = listHelper.getEmbedded();
this.propertiesLocal = listHelper.getLocal();
this.unidirectional = listHelper.getUnidirectional();
@@ -436,12 +433,6 @@ public class BeanDescriptor<T> implements MetaBeanInfo {
this.derivedTableJoins = listHelper.getTableJoin();
if (propertiesId.length == 1) {
this.propertySingleId = propertiesId[0];
} else {
this.propertySingleId = null;
}
// Check if there are no cascade save associated beans ( subject to change
// in initialiseOther()). Note that if we are in an inheritance hierarchy
// then we also need to check every BeanDescriptors in the InheritInfo as
@@ -454,9 +445,20 @@ public class BeanDescriptor<T> implements MetaBeanInfo {
deleteRecurseSkippable = (0 == (propertiesOneExportedDelete.length + propertiesOneImportedDelete.length + propertiesManyDelete.length));
// object used to handle Id values
this.idBinder = owner.createIdBinder(propertiesId);
}
this.idBinder = owner.createIdBinder(idProperty);
// derive the index position of the Id and Version properties
if (Modifier.isAbstract(beanType.getModifiers())) {
this.idPropertyIndex = -1;
this.versionPropertyIndex = -1;
} else {
EntityBean entityBean = createEntityBean();
EntityBeanIntercept ebi = entityBean._ebean_getIntercept();
this.idPropertyIndex = (idProperty == null) ? -1 : ebi.findProperty(idProperty.getName());
this.versionPropertyIndex = (versionProperty == null) ? -1 : ebi.findProperty(versionProperty.getName());
}
}
private LinkedHashMap<String, BeanProperty> getReverseMap(LinkedHashMap<String, BeanProperty> propMap) {
LinkedHashMap<String, BeanProperty> revMap = new LinkedHashMap<String, BeanProperty>(propMap.size() * 2);
@@ -523,26 +525,6 @@ public class BeanDescriptor<T> implements MetaBeanInfo {
return dirtyProperties;
}
/**
* Determine the non-null properties of the bean.
*/
public Set<String> determineLoadedProperties(EntityBean bean) {
HashSet<String> nonNullProps = new HashSet<String>();
for (int j = 0; j < propertiesId.length; j++) {
if (propertiesId[j].getValue(bean) != null) {
nonNullProps.add(propertiesId[j].getName());
}
}
for (int i = 0; i < propertiesNonTransient.length; i++) {
if (propertiesNonTransient[i].getValue(bean) != null) {
nonNullProps.add(propertiesNonTransient[i].getName());
}
}
return nonNullProps;
}
/**
* Return the EbeanServer instance that owns this BeanDescriptor.
*/
@@ -591,9 +573,8 @@ public class BeanDescriptor<T> implements MetaBeanInfo {
}
} else {
// initialise just the Id properties
BeanProperty[] idProps = propertiesId();
for (int i = 0; i < idProps.length; i++) {
idProps[i].initialise();
if (idProperty != null) {
idProperty.initialise();
}
}
}
@@ -1367,7 +1348,7 @@ public class BeanDescriptor<T> implements MetaBeanInfo {
// Note: not creating proxies for many's...
ebi.setReference();
ebi.setReference(idPropertyIndex);
return (T) eb;
@@ -1508,20 +1489,7 @@ public class BeanDescriptor<T> implements MetaBeanInfo {
* properties that make up the unique id.
*/
public Object getId(EntityBean bean) {
if (propertySingleId != null) {
return propertySingleId.getValue(bean);
}
// it is a concatenated id Not embedded
// so return a Map
LinkedHashMap<String, Object> idMap = new LinkedHashMap<String, Object>();
for (int i = 0; i < propertiesId.length; i++) {
Object value = propertiesId[i].getValue(bean);
idMap.put(propertiesId[i].getName(), value);
}
return idMap;
return (idProperty == null) ? null : idProperty.getValue(bean);
}
/**
@@ -2013,17 +1981,6 @@ public class BeanDescriptor<T> implements MetaBeanInfo {
return propMap.values().iterator();
}
/**
* Return the BeanProperty that make up the unique id.
* <p>
* The order of these properties can be relied on to be consistent if the bean
* itself doesn't change or the xml deployment order does not change.
* </p>
*/
public BeanProperty[] propertiesId() {
return propertiesId;
}
/**
* Return the non transient non id properties.
*/
@@ -2038,14 +1995,6 @@ public class BeanDescriptor<T> implements MetaBeanInfo {
return propertiesTransient;
}
/**
* If the Id is a single non-embedded property then returns that, otherwise
* returns null.
*/
public BeanProperty getSingleIdProperty() {
return propertySingleId;
}
/**
* Return the beans that are embedded. These share the base table with the
* owner bean.
@@ -2054,6 +2003,51 @@ public class BeanDescriptor<T> implements MetaBeanInfo {
return propertiesEmbedded;
}
public BeanProperty getIdProperty() {
return idProperty;
}
public boolean isInsertMode(EntityBeanIntercept ebi) {
if (idProperty.isEmbedded()) {
return !ebi.isLoaded();
}
//if (idGenerator == null) {
// return !ebi.isLoaded();
//} else {
return !hasIdProperty(ebi);
//}
}
public boolean isReference(EntityBeanIntercept ebi) {
return ebi.isReference() || hasIdPropertyOnly(ebi);
}
public boolean hasIdPropertyOnly(EntityBeanIntercept ebi) {
return ebi.hasIdOnly(idPropertyIndex);
}
public boolean hasIdProperty(EntityBeanIntercept ebi) {
if (idPropertyIndex > -1) {
return ebi.isLoadedProperty(idPropertyIndex);
}
return false;
}
public boolean hasVersionProperty(EntityBeanIntercept ebi) {
if (versionPropertyIndex > -1) {
return ebi.isLoadedProperty(versionPropertyIndex);
}
return false;
}
public ConcurrencyMode getConcurrencyMode(EntityBeanIntercept ebi) {
if (!hasVersionProperty(ebi)) {
return ConcurrencyMode.NONE;
} else {
return concurrencyMode;
}
}
/**
* All the BeanPropertyAssocOne that are not embedded. These are effectively
* joined beans. For ManyToOne and OneToOne associations.
@@ -2248,8 +2242,6 @@ public class BeanDescriptor<T> implements MetaBeanInfo {
@SuppressWarnings("unchecked")
private void jsonWriteProperties(WriteJsonContext ctx, EntityBean bean) {
boolean referenceBean = ctx.isReferenceBean();
JsonWriteBeanVisitor<T> beanVisitor = (JsonWriteBeanVisitor<T>) ctx.getBeanVisitor();
Set<String> props = ctx.getIncludeProperties();
@@ -2264,11 +2256,11 @@ public class BeanDescriptor<T> implements MetaBeanInfo {
}
}
for (int i = 0; i < propertiesId.length; i++) {
Object idValue = propertiesId[i].getValue(bean);
if (idProperty != null) {
Object idValue = idProperty.getValue(bean);
if (idValue != null) {
if (props == null || props.contains(propertiesId[i].getName())) {
propertiesId[i].jsonWrite(ctx, bean);
if (props == null || props.contains(idProperty.getName())) {
idProperty.jsonWrite(ctx, bean);
}
}
}
@@ -2286,7 +2278,7 @@ public class BeanDescriptor<T> implements MetaBeanInfo {
}
}
} else {
if (explicitAllProps || !referenceBean) {
if (explicitAllProps || !isReference(bean._ebean_getIntercept())) {
// render all the properties and invoke lazy loading if required
for (int j = 0; j < propertiesNonTransient.length; j++) {
propertiesNonTransient[j].jsonWrite(ctx, bean);
@@ -2386,44 +2378,11 @@ public class BeanDescriptor<T> implements MetaBeanInfo {
return ctx.popBeanState();
}
/**
* Set the loaded properties with additional check to see if the bean is a
* reference.
*/
public void setLoadedProps(EntityBeanIntercept ebi, Set<String> loadedProps) {
if (isLoadedReference(loadedProps)) {
ebi.setReference();
} else {
ebi.setLoaded();
}
}
/**
* Return true if the loadedProperties is just the Id property and therefore
* this is really a reference.
*/
public boolean isLoadedReference(Set<String> loadedProps) {
if (loadedProps != null) {
if (loadedProps.size() == propertiesId.length) {
for (int i = 0; i < propertiesId.length; i++) {
if (!loadedProps.contains(propertiesId[i].getName())) {
return false;
}
}
return true;
}
}
return false;
}
public void flushPersistenceContextOnIterate(PersistenceContext persistenceContext) {
persistenceContext.clear(beanType);
for (int i = 0; i < propertiesMany.length; i++) {
persistenceContext.clear(propertiesMany[i].getBeanDescriptor().getBeanType());
}
}
}
@@ -222,8 +222,8 @@ public class BeanDescriptorManager implements BeanDescriptorMap {
}
}
public IdBinder createIdBinder(BeanProperty[] uids) {
return idBinderFactory.createIdBinder(uids);
public IdBinder createIdBinder(BeanProperty idProperty) {
return idBinderFactory.createIdBinder(idProperty);
}
public void deploy() {
@@ -32,6 +32,6 @@ public interface BeanDescriptorMap {
*/
public EncryptKey getEncryptKey(String tableName, String columnName);
public IdBinder createIdBinder(BeanProperty[] uids);
public IdBinder createIdBinder(BeanProperty id);
}
@@ -168,7 +168,7 @@ public final class BeanFkeyProperty implements ElPropertyValue {
throw new RuntimeException("ElPropertyDeploy only - not implemented");
}
public void elSetValue(EntityBean bean, Object value, boolean populate, boolean reference) {
public void elSetValue(EntityBean bean, Object value, boolean populate) {
throw new RuntimeException("ElPropertyDeploy only - not implemented");
}
@@ -757,11 +757,7 @@ public class BeanProperty implements ElPropertyValue {
return convertToLogicalType(value);
}
public void elSetReference(EntityBean bean) {
throw new RuntimeException("Should not be called");
}
public void elSetValue(EntityBean bean, Object value, boolean populate, boolean reference) {
public void elSetValue(EntityBean bean, Object value, boolean populate) {
if (bean != null) {
// Not using setValueIntercept at this stage
setValue(bean, value);
@@ -4,18 +4,18 @@ import java.util.ArrayList;
import javax.persistence.PersistenceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebeaninternal.server.core.InternString;
import com.avaje.ebeaninternal.server.deploy.id.IdBinder;
import com.avaje.ebeaninternal.server.deploy.id.ImportedId;
import com.avaje.ebeaninternal.server.deploy.id.ImportedIdEmbedded;
import com.avaje.ebeaninternal.server.deploy.id.ImportedIdMultiple;
import com.avaje.ebeaninternal.server.deploy.id.ImportedIdSimple;
import com.avaje.ebeaninternal.server.deploy.meta.DeployBeanPropertyAssoc;
import com.avaje.ebeaninternal.server.el.ElPropertyChainBuilder;
import com.avaje.ebeaninternal.server.el.ElPropertyValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Abstract base for properties mapped to an associated bean, list, set or map.
@@ -219,11 +219,9 @@ public abstract class BeanPropertyAssoc<T> extends BeanProperty {
public boolean hasId(EntityBean bean) {
BeanDescriptor<?> targetDesc = getTargetDescriptor();
BeanProperty[] uids = targetDesc.propertiesId();
for (int i = 0; i < uids.length; i++) {
Object value = uids[i].getValue(bean);
BeanProperty idProp = targetDesc.getIdProperty();
if (idProp != null) {
Object value = idProp.getValue(bean);
if (value == null) {
return false;
}
@@ -311,39 +309,36 @@ public abstract class BeanPropertyAssoc<T> extends BeanProperty {
*/
protected ImportedId createImportedId(BeanPropertyAssoc<?> owner, BeanDescriptor<?> target, TableJoin join) {
BeanProperty[] props = target.propertiesId();
BeanProperty idProp = target.getIdProperty();
BeanProperty[] others = target.propertiesBaseScalar();
if (descriptor.isSqlSelectBased()){
String dbColumn = owner.getDbColumn();
return new ImportedIdSimple(owner, dbColumn, props[0], 0);
return new ImportedIdSimple(owner, dbColumn, idProp, 0);
}
TableJoinColumn[] cols = join.columns();
if (props.length == 1) {
if (!props[0].isEmbedded()) {
// simple single scalar id
if (cols.length != 1){
String msg = "No Imported Id column for ["+props[0]+"] in table ["+join.getTable()+"]";
logger.error(msg);
return null;
} else {
return createImportedScalar(owner, cols[0], props, others);
}
if (idProp == null) {
return null;
}
if (!idProp.isEmbedded()) {
// simple single scalar id
if (cols.length != 1){
String msg = "No Imported Id column for ["+idProp+"] in table ["+join.getTable()+"]";
logger.error(msg);
return null;
} else {
// embedded id
BeanPropertyAssocOne<?> embProp = (BeanPropertyAssocOne<?>)props[0];
BeanProperty[] embBaseProps = embProp.getTargetDescriptor().propertiesBaseScalar();
ImportedIdSimple[] scalars = createImportedList(owner, cols, embBaseProps, others);
return new ImportedIdEmbedded(owner, embProp, scalars);
BeanProperty[] idProps = {idProp};
return createImportedScalar(owner, cols[0], idProps, others);
}
} else {
// Concatenated key that is not embedded
ImportedIdSimple[] scalars = createImportedList(owner, cols, props, others);
return new ImportedIdMultiple(owner, scalars);
// embedded id
BeanPropertyAssocOne<?> embProp = (BeanPropertyAssocOne<?>)idProp;
BeanProperty[] embBaseProps = embProp.getTargetDescriptor().propertiesBaseScalar();
ImportedIdSimple[] scalars = createImportedList(owner, cols, embBaseProps, others);
return new ImportedIdEmbedded(owner, embProp, scalars);
}
}
@@ -588,8 +588,8 @@ public class BeanPropertyAssocMany<T> extends BeanPropertyAssoc<T> {
if (embeddedExportedProperties) {
// use the EmbeddedId object instead of the parentBean
BeanProperty[] uids = descriptor.propertiesId();
parentBean = (EntityBean)uids[0].getValue(parentBean);
BeanProperty idProp = descriptor.getIdProperty();
parentBean = (EntityBean)idProp.getValue(parentBean);
}
for (int i = 0; i < exportedProperties.length; i++) {
@@ -621,13 +621,13 @@ public class BeanPropertyAssocMany<T> extends BeanPropertyAssoc<T> {
*/
private ExportedProperty[] createExported() {
BeanProperty[] uids = descriptor.propertiesId();
BeanProperty idProp = descriptor.getIdProperty();
ArrayList<ExportedProperty> list = new ArrayList<ExportedProperty>();
if (uids.length == 1 && uids[0].isEmbedded()) {
if (idProp != null && idProp.isEmbedded()) {
BeanPropertyAssocOne<?> one = (BeanPropertyAssocOne<?>) uids[0];
BeanPropertyAssocOne<?> one = (BeanPropertyAssocOne<?>) idProp;
BeanDescriptor<?> targetDesc = one.getTargetDescriptor();
BeanProperty[] emIds = targetDesc.propertiesBaseScalar();
try {
@@ -641,8 +641,8 @@ public class BeanPropertyAssocMany<T> extends BeanPropertyAssoc<T> {
}
} else {
for (int i = 0; i < uids.length; i++) {
ExportedProperty expProp = findMatch(false, uids[i]);
if (idProp != null) {
ExportedProperty expProp = findMatch(false, idProp);
list.add(expProp);
}
}
@@ -773,8 +773,8 @@ public class BeanPropertyAssocMany<T> extends BeanPropertyAssoc<T> {
private void buildExport(IntersectionRow row, EntityBean parentBean) {
if (embeddedExportedProperties) {
BeanProperty[] uids = descriptor.propertiesId();
parentBean = (EntityBean)uids[0].getValue(parentBean);
BeanProperty idProp = descriptor.getIdProperty();
parentBean = (EntityBean)idProp.getValue(parentBean);
}
for (int i = 0; i < exportedProperties.length; i++) {
Object val = exportedProperties[i].getValue(parentBean);
@@ -856,6 +856,5 @@ public class BeanPropertyAssocMany<T> extends BeanPropertyAssoc<T> {
} while(true);
setValue(bean, collection);
}
}
@@ -397,13 +397,6 @@ public class BeanPropertyAssocOne<T> extends BeanPropertyAssoc<T> {
return targetDescriptor.createEntityBean();
}
public void elSetReference(EntityBean bean) {
Object value = getValueIntercept(bean);
if (value != null) {
((EntityBean) value)._ebean_getIntercept().setReference();
}
}
@Override
public Object elGetReference(EntityBean bean) {
Object value = getValueIntercept(bean);
@@ -441,13 +434,13 @@ public class BeanPropertyAssocOne<T> extends BeanPropertyAssoc<T> {
*/
private ExportedProperty[] createExported() {
BeanProperty[] uids = descriptor.propertiesId();
BeanProperty idProp = descriptor.getIdProperty();
ArrayList<ExportedProperty> list = new ArrayList<ExportedProperty>();
if (uids.length == 1 && uids[0].isEmbedded()) {
if (idProp != null && idProp.isEmbedded()) {
BeanPropertyAssocOne<?> one = (BeanPropertyAssocOne<?>) uids[0];
BeanPropertyAssocOne<?> one = (BeanPropertyAssocOne<?>) idProp;
BeanDescriptor<?> targetDesc = one.getTargetDescriptor();
BeanProperty[] emIds = targetDesc.propertiesBaseScalar();
try {
@@ -461,8 +454,8 @@ public class BeanPropertyAssocOne<T> extends BeanPropertyAssoc<T> {
}
} else {
for (int i = 0; i < uids.length; i++) {
ExportedProperty expProp = findMatch(false, uids[i]);
if (idProp != null) {
ExportedProperty expProp = findMatch(false, idProp);
list.add(expProp);
}
}
@@ -806,8 +799,7 @@ public class BeanPropertyAssocOne<T> extends BeanPropertyAssoc<T> {
@Override
void appendSelect(DbSqlContext ctx, boolean subQuery) {
// set appropriate tableAlias for
// the exported id columns
// set appropriate tableAlias for the exported id columns
String relativePrefix = ctx.getRelativePrefix(getName());
ctx.pushTableAlias(relativePrefix);
@@ -838,20 +830,29 @@ public class BeanPropertyAssocOne<T> extends BeanPropertyAssoc<T> {
// bi-directional and already rendered parent
} else {
// Hmmm, not writing complex non-entity bean
if (value instanceof EntityBean) {
ctx.pushParentBean(bean);
ctx.beginAssocOne(name);
BeanDescriptor<?> refDesc = descriptor.getBeanDescriptor(value.getClass());
refDesc.jsonWrite(ctx, (EntityBean)value);
refDesc.jsonWrite(ctx, (EntityBean)value);
ctx.endAssocOne();
ctx.popParentBean();
}
}
}
}
@Override
public void jsonRead(ReadJsonContext ctx, EntityBean bean){
T assocBean = targetDescriptor.jsonReadBean(ctx, name);
setValue(bean, assocBean);
if (targetDescriptor != null) {
T assocBean = targetDescriptor.jsonReadBean(ctx, name);
setValue(bean, assocBean);
}
}
public boolean isReference(Object detailBean) {
EntityBean eb = (EntityBean)detailBean;
return targetDescriptor.isReference(eb._ebean_getIntercept());
}
}
@@ -90,13 +90,8 @@ public class BeanPropertyCompoundScalar extends BeanProperty {
}
@Override
public void elSetReference(EntityBean bean) {
super.elSetReference(bean);
}
@Override
public void elSetValue(EntityBean bean, Object value, boolean populate, boolean reference) {
super.elSetValue(bean, value, populate, reference);
public void elSetValue(EntityBean bean, Object value, boolean populate) {//, boolean reference) {
super.elSetValue(bean, value, populate);
}
@@ -193,11 +193,10 @@ public final class DRawSqlSelectColumnsParser {
}
}
BeanProperty[] propertiesId = desc.propertiesId();
for (int i = 0; i < propertiesId.length; i++) {
BeanProperty prop = propertiesId[i];
if (isMatch(prop, searchColumn)) {
return prop;
BeanProperty idProp = desc.getIdProperty();
if (idProp != null) {
if (isMatch(idProp, searchColumn)) {
return idProp;
}
}
@@ -44,6 +44,11 @@ public interface IdBinder {
*/
public String getIdProperty();
/**
* Return the Id BeanProperty.
*/
public BeanProperty getBeanProperty();
/**
* Find a BeanProperty that is mapped to the database column.
*/
@@ -154,11 +159,6 @@ public interface IdBinder {
*/
public String getBindIdSql(String baseTableAlias);
/**
* Return the id properties in flat form.
*/
public BeanProperty[] getProperties();
/**
* Cast or convert the Id value if necessary and optionally set it.
* <p>
@@ -6,8 +6,6 @@ import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import javax.persistence.PersistenceException;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import com.avaje.ebeaninternal.server.core.DefaultSqlUpdate;
@@ -24,378 +22,382 @@ import com.avaje.ebeaninternal.server.type.DataBind;
*/
public final class IdBinderEmbedded implements IdBinder {
private final BeanPropertyAssocOne<?> embIdProperty;
private final BeanPropertyAssocOne<?> embIdProperty;
private final boolean idInExpandedForm;
private BeanProperty[] props;
private final boolean idInExpandedForm;
private BeanDescriptor<?> idDesc;
private BeanProperty[] props;
private String idInValueSql;
public IdBinderEmbedded(boolean idInExpandedForm, BeanPropertyAssocOne<?> embIdProperty) {
this.idInExpandedForm = idInExpandedForm;
this.embIdProperty = embIdProperty;
private BeanDescriptor<?> idDesc;
private String idInValueSql;
public IdBinderEmbedded(boolean idInExpandedForm, BeanPropertyAssocOne<?> embIdProperty) {
this.idInExpandedForm = idInExpandedForm;
this.embIdProperty = embIdProperty;
}
public void initialise() {
this.idDesc = embIdProperty.getTargetDescriptor();
this.props = embIdProperty.getProperties();
this.idInValueSql = idInExpandedForm ? idInExpanded() : idInCompressed();
}
private String idInExpanded() {
StringBuilder sb = new StringBuilder();
sb.append("(");
for (int i = 0; i < props.length; i++) {
if (i > 0) {
sb.append(" and ");
}
sb.append(embIdProperty.getName());
sb.append(".");
sb.append(props[i].getName());
sb.append("=?");
}
sb.append(")");
return sb.toString();
}
private String idInCompressed() {
StringBuilder sb = new StringBuilder();
sb.append("(");
for (int i = 0; i < props.length; i++) {
if (i > 0) {
sb.append(",");
}
sb.append("?");
}
sb.append(")");
return sb.toString();
}
@Override
public BeanProperty getBeanProperty() {
return embIdProperty;
}
public String getOrderBy(String pathPrefix, boolean ascending) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < props.length; i++) {
if (i > 0) {
sb.append(", ");
}
if (pathPrefix != null) {
sb.append(pathPrefix).append(".");
}
sb.append(embIdProperty.getName()).append(".");
sb.append(props[i].getName());
if (!ascending) {
sb.append(" desc");
}
}
return sb.toString();
}
public BeanDescriptor<?> getIdBeanDescriptor() {
return idDesc;
}
public int getPropertyCount() {
return props.length;
}
public String getIdProperty() {
return embIdProperty.getName();
}
public void buildSelectExpressionChain(String prefix, List<String> selectChain) {
prefix = SplitName.add(prefix, embIdProperty.getName());
for (int i = 0; i < props.length; i++) {
props[i].buildSelectExpressionChain(prefix, selectChain);
}
}
public BeanProperty findBeanProperty(String dbColumnName) {
for (int i = 0; i < props.length; i++) {
if (dbColumnName.equalsIgnoreCase(props[i].getDbColumn())) {
return props[i];
}
}
return null;
}
public boolean isComplexId() {
return true;
}
public String getDefaultOrderBy() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < props.length; i++) {
if (i > 0) {
sb.append(",");
}
sb.append(embIdProperty.getName());
sb.append(".");
sb.append(props[i].getName());
}
public void initialise() {
this.idDesc = embIdProperty.getTargetDescriptor();
this.props = embIdProperty.getProperties();
this.idInValueSql = idInExpandedForm ? idInExpanded() : idInCompressed();
return sb.toString();
}
public BeanProperty[] getProperties() {
return props;
}
public void addIdInBindValue(SpiExpressionRequest request, Object value) {
for (int i = 0; i < props.length; i++) {
request.addBindValue(props[i].getValue((EntityBean) value));
}
}
public String getIdInValueExprDelete(int size) {
if (!idInExpandedForm) {
return getIdInValueExpr(size);
}
private String idInExpanded() {
StringBuilder sb = new StringBuilder();
sb.append("(");
for (int i = 0; i < props.length; i++) {
if (i > 0) {
sb.append(" and ");
}
sb.append(embIdProperty.getName());
sb.append(".");
sb.append(props[i].getName());
sb.append("=?");
StringBuilder sb = new StringBuilder();
sb.append("(");
for (int j = 0; j < size; j++) {
if (j > 0) {
sb.append(" or ");
}
sb.append("(");
for (int i = 0; i < props.length; i++) {
if (i > 0) {
sb.append(" and ");
}
sb.append(")");
return sb.toString();
sb.append(props[i].getDbColumn());
sb.append("=?");
}
sb.append(")");
}
private String idInCompressed() {
StringBuilder sb = new StringBuilder();
sb.append("(");
for (int i = 0; i < props.length; i++) {
if (i > 0) {
sb.append(",");
}
sb.append("?");
}
sb.append(")");
sb.append(") ");
return sb.toString();
}
return sb.toString();
public String getIdInValueExpr(int size) {
StringBuilder sb = new StringBuilder();
if (!idInExpandedForm) {
sb.append(" in");
}
public String getOrderBy(String pathPrefix, boolean ascending){
StringBuilder sb = new StringBuilder();
for (int i = 0; i < props.length; i++) {
if (i > 0) {
sb.append(", ");
}
if (pathPrefix != null){
sb.append(pathPrefix).append(".");
}
sb.append(embIdProperty.getName()).append(".");
sb.append(props[i].getName());
if (!ascending){
sb.append(" desc");
}
}
return sb.toString();
}
public BeanDescriptor<?> getIdBeanDescriptor() {
return idDesc;
}
public int getPropertyCount() {
return props.length;
}
public String getIdProperty() {
return embIdProperty.getName();
}
public void buildSelectExpressionChain(String prefix, List<String> selectChain) {
prefix = SplitName.add(prefix, embIdProperty.getName());
for (int i = 0; i < props.length; i++) {
props[i].buildSelectExpressionChain(prefix, selectChain);
}
}
public BeanProperty findBeanProperty(String dbColumnName) {
for (int i = 0; i < props.length; i++) {
if (dbColumnName.equalsIgnoreCase(props[i].getDbColumn())) {
return props[i];
}
}
return null;
}
public boolean isComplexId() {
return true;
}
public String getDefaultOrderBy() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < props.length; i++) {
if (i > 0) {
sb.append(",");
}
sb.append(embIdProperty.getName());
sb.append(".");
sb.append(props[i].getName());
}
return sb.toString();
}
public BeanProperty[] getProperties() {
return props;
}
public void addIdInBindValue(SpiExpressionRequest request, Object value) {
for (int i = 0; i < props.length; i++) {
request.addBindValue(props[i].getValue((EntityBean)value));
}
}
public String getIdInValueExprDelete(int size) {
if (!idInExpandedForm){
return getIdInValueExpr(size);
}
StringBuilder sb = new StringBuilder();
sb.append("(");
for (int j = 0; j < size; j++) {
if (j > 0){
sb.append(" or ");
}
sb.append("(");
for (int i = 0; i < props.length; i++) {
if (i > 0) {
sb.append(" and ");
}
sb.append(props[i].getDbColumn());
sb.append("=?");
}
sb.append(")");
}
sb.append(") ");
return sb.toString();
}
public String getIdInValueExpr(int size) {
StringBuilder sb = new StringBuilder();
if (!idInExpandedForm){
sb.append(" in");
}
sb.append(" (");
for (int i = 0; i < size; i++) {
if (i > 0){
if (idInExpandedForm) {
sb.append(" or ");
} else {
sb.append(",");
}
}
sb.append(idInValueSql);
}
sb.append(") ");
return sb.toString();
}
public String getIdInValueExpr() {
return idInValueSql;
}
public Object[] getIdValues(EntityBean bean) {
Object val = embIdProperty.getValue(bean);
Object[] bindvalues = new Object[props.length];
for (int i = 0; i < props.length; i++) {
bindvalues[i] = props[i].getValue((EntityBean)val);
}
return bindvalues;
}
public Object[] getBindValues(Object value) {
Object[] bindvalues = new Object[props.length];
for (int i = 0; i < props.length; i++) {
bindvalues[i] = props[i].getValue((EntityBean)value);
}
return bindvalues;
}
public void bindId(DefaultSqlUpdate sqlUpdate, Object value) {
for (int i = 0; i < props.length; i++) {
Object embFieldValue = props[i].getValue((EntityBean)value);
sqlUpdate.addParameter(embFieldValue);
}
}
public void bindId(DataBind dataBind, Object value) throws SQLException {
for (int i = 0; i < props.length; i++) {
Object embFieldValue = props[i].getValue((EntityBean)value);
props[i].bind(dataBind, embFieldValue);
}
}
public Object readData(DataInput dataInput) throws IOException {
EntityBean embId = idDesc.createBean();
boolean notNull = true;
for (int i = 0; i < props.length; i++) {
Object value = props[i].readData(dataInput);
props[i].setValue(embId, value);
if (value == null) {
notNull = false;
}
}
if (notNull) {
return embId;
sb.append(" (");
for (int i = 0; i < size; i++) {
if (i > 0) {
if (idInExpandedForm) {
sb.append(" or ");
} else {
return null;
sb.append(",");
}
}
sb.append(idInValueSql);
}
sb.append(") ");
return sb.toString();
}
public String getIdInValueExpr() {
return idInValueSql;
}
public Object[] getIdValues(EntityBean bean) {
Object val = embIdProperty.getValue(bean);
Object[] bindvalues = new Object[props.length];
for (int i = 0; i < props.length; i++) {
bindvalues[i] = props[i].getValue((EntityBean) val);
}
return bindvalues;
}
public Object[] getBindValues(Object value) {
Object[] bindvalues = new Object[props.length];
for (int i = 0; i < props.length; i++) {
bindvalues[i] = props[i].getValue((EntityBean) value);
}
return bindvalues;
}
public void bindId(DefaultSqlUpdate sqlUpdate, Object value) {
for (int i = 0; i < props.length; i++) {
Object embFieldValue = props[i].getValue((EntityBean) value);
sqlUpdate.addParameter(embFieldValue);
}
}
public void bindId(DataBind dataBind, Object value) throws SQLException {
for (int i = 0; i < props.length; i++) {
Object embFieldValue = props[i].getValue((EntityBean) value);
props[i].bind(dataBind, embFieldValue);
}
}
public Object readData(DataInput dataInput) throws IOException {
EntityBean embId = idDesc.createBean();
boolean notNull = true;
for (int i = 0; i < props.length; i++) {
Object value = props[i].readData(dataInput);
props[i].setValue(embId, value);
if (value == null) {
notNull = false;
}
}
public void writeData(DataOutput dataOutput, Object idValue) throws IOException {
for (int i = 0; i < props.length; i++) {
Object embFieldValue = props[i].getValue((EntityBean)idValue);
props[i].writeData(dataOutput, embFieldValue);
}
if (notNull) {
return embId;
} else {
return null;
}
}
public void writeData(DataOutput dataOutput, Object idValue) throws IOException {
for (int i = 0; i < props.length; i++) {
Object embFieldValue = props[i].getValue((EntityBean) idValue);
props[i].writeData(dataOutput, embFieldValue);
}
}
public void loadIgnore(DbReadContext ctx) {
for (int i = 0; i < props.length; i++) {
props[i].loadIgnore(ctx);
}
}
public Object read(DbReadContext ctx) throws SQLException {
EntityBean embId = idDesc.createBean();
boolean notNull = true;
for (int i = 0; i < props.length; i++) {
Object value = props[i].readSet(ctx, embId, null);
if (value == null) {
notNull = false;
}
}
public void loadIgnore(DbReadContext ctx) {
for (int i = 0; i < props.length; i++) {
props[i].loadIgnore(ctx);
}
if (notNull) {
return embId;
} else {
return null;
}
}
public Object readSet(DbReadContext ctx, EntityBean bean) throws SQLException {
Object embId = read(ctx);
if (embId != null) {
embIdProperty.setValue(bean, embId);
return embId;
} else {
return null;
}
}
public void appendSelect(DbSqlContext ctx, boolean subQuery) {
for (int i = 0; i < props.length; i++) {
props[i].appendSelect(ctx, subQuery);
}
}
public String getAssocIdInExpr(String prefix) {
StringBuilder sb = new StringBuilder();
sb.append("(");
for (int i = 0; i < props.length; i++) {
if (i > 0) {
sb.append(",");
}
if (prefix != null) {
sb.append(prefix);
sb.append(".");
}
sb.append(props[i].getName());
}
sb.append(")");
return sb.toString();
}
public String getAssocOneIdExpr(String prefix, String operator) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < props.length; i++) {
if (i > 0) {
sb.append(" and ");
}
if (prefix != null) {
sb.append(prefix);
sb.append(".");
}
sb.append(embIdProperty.getName());
sb.append(".");
sb.append(props[i].getName());
sb.append(operator);
}
return sb.toString();
}
public String getBindIdSql(String baseTableAlias) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < props.length; i++) {
if (i > 0) {
sb.append(" and ");
}
if (baseTableAlias != null) {
sb.append(baseTableAlias);
sb.append(".");
}
sb.append(props[i].getDbColumn());
sb.append(" = ? ");
}
return sb.toString();
}
public String getBindIdInSql(String baseTableAlias) {
if (idInExpandedForm) {
return "";
}
public Object read(DbReadContext ctx) throws SQLException {
StringBuilder sb = new StringBuilder();
sb.append("(");
for (int i = 0; i < props.length; i++) {
if (i > 0) {
sb.append(",");
}
if (baseTableAlias != null) {
sb.append(baseTableAlias);
sb.append(".");
}
sb.append(props[i].getDbColumn());
}
sb.append(")");
return sb.toString();
}
EntityBean embId = idDesc.createBean();
boolean notNull = true;
public Object convertSetId(Object idValue, EntityBean bean) {
for (int i = 0; i < props.length; i++) {
Object value = props[i].readSet(ctx, embId, null);
if (value == null) {
notNull = false;
}
}
if (notNull) {
return embId;
} else {
return null;
}
// can not cast/convert if it is embedded
if (bean != null) {
// support PropertyChangeSupport
embIdProperty.setValueIntercept(bean, idValue);
}
public Object readSet(DbReadContext ctx, EntityBean bean) throws SQLException {
Object embId = read(ctx);
if (embId != null) {
embIdProperty.setValue(bean, embId);
return embId;
} else {
return null;
}
}
public void appendSelect(DbSqlContext ctx, boolean subQuery) {
for (int i = 0; i < props.length; i++) {
props[i].appendSelect(ctx, subQuery);
}
}
public String getAssocIdInExpr(String prefix) {
StringBuilder sb = new StringBuilder();
sb.append("(");
for (int i = 0; i < props.length; i++) {
if (i > 0) {
sb.append(",");
}
if (prefix != null) {
sb.append(prefix);
sb.append(".");
}
sb.append(props[i].getName());
}
sb.append(")");
return sb.toString();
}
public String getAssocOneIdExpr(String prefix, String operator) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < props.length; i++) {
if (i > 0) {
sb.append(" and ");
}
if (prefix != null) {
sb.append(prefix);
sb.append(".");
}
sb.append(embIdProperty.getName());
sb.append(".");
sb.append(props[i].getName());
sb.append(operator);
}
return sb.toString();
}
public String getBindIdSql(String baseTableAlias) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < props.length; i++) {
if (i > 0) {
sb.append(" and ");
}
if (baseTableAlias != null) {
sb.append(baseTableAlias);
sb.append(".");
}
sb.append(props[i].getDbColumn());
sb.append(" = ? ");
}
return sb.toString();
}
public String getBindIdInSql(String baseTableAlias) {
if (idInExpandedForm){
return "";
}
StringBuilder sb = new StringBuilder();
sb.append("(");
for (int i = 0; i < props.length; i++) {
if (i > 0) {
sb.append(",");
}
if (baseTableAlias != null){
sb.append(baseTableAlias);
sb.append(".");
}
sb.append(props[i].getDbColumn());
}
sb.append(")");
return sb.toString();
}
public Object convertSetId(Object idValue, EntityBean bean) {
// can not cast/convert if it is embedded
if (bean != null) {
// support PropertyChangeSupport
embIdProperty.setValueIntercept(bean, idValue);
}
return idValue;
}
return idValue;
}
}
@@ -22,8 +22,6 @@ public final class IdBinderEmpty implements IdBinder {
private static final String bindIdSql = "";
private static final BeanProperty[] properties = new BeanProperty[0];
public IdBinderEmpty() {
}
@@ -43,6 +41,11 @@ public final class IdBinderEmpty implements IdBinder {
return 0;
}
@Override
public BeanProperty getBeanProperty() {
return null;
}
public String getIdProperty() {
return null;
}
@@ -60,10 +63,6 @@ public final class IdBinderEmpty implements IdBinder {
return "";
}
public BeanProperty[] getProperties() {
return properties;
}
public String getBindIdSql(String baseTableAlias) {
return bindIdSql;
}
@@ -19,21 +19,17 @@ public class IdBinderFactory {
/**
* Create the IdConvertSet for the given type of Id properties.
*/
public IdBinder createIdBinder(BeanProperty[] uids) {
public IdBinder createIdBinder(BeanProperty id) {
if (uids.length == 0){
if (id == null){
// for report type beans that don't need an id
return EMPTY;
} else if (uids.length == 1){
if (uids[0].isEmbedded()){
return new IdBinderEmbedded(idInExpandedForm, (BeanPropertyAssocOne<?>)uids[0]);
} else {
return new IdBinderSimple(uids[0]);
}
}
if (id.isEmbedded()){
return new IdBinderEmbedded(idInExpandedForm, (BeanPropertyAssocOne<?>)id);
} else {
return new IdBinderMultiple(uids);
return new IdBinderSimple(id);
}
}
@@ -1,399 +0,0 @@
package com.avaje.ebeaninternal.server.deploy.id;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.PersistenceException;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import com.avaje.ebeaninternal.server.core.DefaultSqlUpdate;
import com.avaje.ebeaninternal.server.core.InternString;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
import com.avaje.ebeaninternal.server.deploy.DbReadContext;
import com.avaje.ebeaninternal.server.deploy.DbSqlContext;
import com.avaje.ebeaninternal.server.lib.util.MapFromString;
import com.avaje.ebeaninternal.server.type.DataBind;
/**
* Bind an Id that is made up of multiple separate properties.
* <p>
* The id passed in for binding is expected to be a map with the key being the
* String name of the property and the value being that properties bind value.
* </p>
*/
public final class IdBinderMultiple implements IdBinder {
private final BeanProperty[] props;
private final String idProperties;
private final String idInValueSql;
public IdBinderMultiple(BeanProperty[] idProps) {
this.props = idProps;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < idProps.length; i++) {
if (i > 0){
sb.append(",");
}
sb.append(idProps[i].getName());
}
idProperties = InternString.intern(sb.toString());
sb = new StringBuilder();
sb.append("(");
for (int i = 0; i < props.length; i++) {
if (i > 0){
sb.append(",");
}
sb.append("?");
}
sb.append(")");
idInValueSql = sb.toString();
}
public void initialise(){
// do nothing
}
public String getOrderBy(String pathPrefix, boolean ascending){
StringBuilder sb = new StringBuilder();
for (int i = 0; i < props.length; i++) {
if (i > 0) {
sb.append(" ");
}
if (pathPrefix != null){
sb.append(pathPrefix).append(".");
}
sb.append(props[i].getName());
if (!ascending){
sb.append(" desc");
}
}
return sb.toString();
}
public void buildSelectExpressionChain(String prefix, List<String> selectChain) {
for (int i = 0; i < props.length; i++) {
props[i].buildSelectExpressionChain(prefix, selectChain);
}
}
public int getPropertyCount() {
return props.length;
}
public String getIdProperty() {
return idProperties;
}
public BeanProperty findBeanProperty(String dbColumnName) {
for (int i = 0; i < props.length; i++) {
if (dbColumnName.equalsIgnoreCase(props[i].getDbColumn())){
return props[i];
}
}
return null;
}
public boolean isComplexId(){
return true;
}
public String getDefaultOrderBy() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < props.length; i++) {
if (i > 0){
sb.append(",");
}
sb.append(props[i].getName());
}
return sb.toString();
}
public BeanProperty[] getProperties() {
return props;
}
public void addIdInBindValue(SpiExpressionRequest request, Object value) {
for (int i = 0; i < props.length; i++) {
request.addBindValue(props[i].getValue((EntityBean)value));
}
}
public String getIdInValueExprDelete(int size) {
return getIdInValueExpr(size);
}
public String getIdInValueExpr(int size) {
StringBuilder sb = new StringBuilder();
sb.append(" in");
sb.append(" (");
sb.append(idInValueSql);
for (int i = 1; i < size; i++) {
sb.append(",").append(idInValueSql);
}
sb.append(") ");
return sb.toString();
}
public String getBindIdInSql(String baseTableAlias) {
StringBuilder sb = new StringBuilder();
sb.append("(");
for (int i = 0; i < props.length; i++) {
if (i > 0) {
sb.append(",");
}
if (baseTableAlias != null){
sb.append(baseTableAlias);
sb.append(".");
}
sb.append(props[i].getDbColumn());
}
sb.append(")");
return sb.toString();
}
public Object[] getIdValues(EntityBean bean){
Object[] bindvalues = new Object[props.length];
for (int i = 0; i < props.length; i++) {
bindvalues[i] = props[i].getValue(bean);
}
return bindvalues;
}
@SuppressWarnings("unchecked")
public Object[] getBindValues(Object idValue){
Object[] bindvalues = new Object[props.length];
// concatenated id as a Map
try {
Map<String, ?> uidMap = (Map<String, ?>) idValue;
for (int i = 0; i < props.length; i++) {
Object value = uidMap.get(props[i].getName());
bindvalues[i] = value;
}
return bindvalues;
} catch (ClassCastException e) {
String msg = "Expecting concatinated idValue to be a Map";
throw new PersistenceException(msg, e);
}
}
public Object readData(DataInput dataInput) throws IOException {
LinkedHashMap<String,Object> map = new LinkedHashMap<String, Object>();
boolean notNull = true;
for (int i = 0; i < props.length; i++) {
Object value = props[i].readData(dataInput);
map.put(props[i].getName(), value);
if (value == null) {
notNull = false;
}
}
if (notNull) {
return map;
} else {
return null;
}
}
@SuppressWarnings("unchecked")
public void writeData(DataOutput dataOutput, Object idValue) throws IOException {
Map<String,Object> map = (Map<String,Object>)idValue;
for (int i = 0; i < props.length; i++) {
Object embFieldValue = map.get(props[i].getName());
//Object embFieldValue = props[i].getValue(idValue);
props[i].writeData(dataOutput, embFieldValue);
}
}
public void loadIgnore(DbReadContext ctx) {
for (int i = 0; i < props.length; i++) {
props[i].loadIgnore(ctx);
}
}
public Object readSet(DbReadContext ctx, EntityBean bean) throws SQLException {
LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
boolean notNull = false;
for (int i = 0; i < props.length; i++) {
Object value = props[i].readSet(ctx, bean, null);
if (value != null){
map.put(props[i].getName(), value);
notNull = true;
}
}
if (notNull){
return map;
} else {
return null;
}
}
public Object read(DbReadContext ctx) throws SQLException {
LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
boolean notNull = false;
for (int i = 0; i < props.length; i++) {
Object value = props[i].read(ctx);
if (value != null){
map.put(props[i].getName(), value);
notNull = true;
}
}
if (notNull){
return map;
} else {
return null;
}
}
@SuppressWarnings("unchecked")
public void bindId(DefaultSqlUpdate sqlUpdate, Object idValue) {
// concatenated id as a Map
try {
Map<String, ?> uidMap = (Map<String, ?>) idValue;
for (int i = 0; i < props.length; i++) {
Object value = uidMap.get(props[i].getName());
sqlUpdate.addParameter(value);
}
} catch (ClassCastException e) {
String msg = "Expecting concatinated idValue to be a Map";
throw new PersistenceException(msg, e);
}
}
@SuppressWarnings("unchecked")
public void bindId(DataBind bind, Object idValue) throws SQLException {
// concatenated id as a Map
try {
Map<String, ?> uidMap = (Map<String, ?>) idValue;
for (int i = 0; i < props.length; i++) {
Object value = uidMap.get(props[i].getName());
props[i].bind(bind, value);
}
} catch (ClassCastException e) {
String msg = "Expecting concatinated idValue to be a Map";
throw new PersistenceException(msg, e);
}
}
public void appendSelect(DbSqlContext ctx, boolean subQuery) {
for (int i = 0; i < props.length; i++) {
props[i].appendSelect(ctx, subQuery);
}
}
public String getAssocIdInExpr(String prefix) {
StringBuilder sb = new StringBuilder();
sb.append("(");
for (int i = 0; i < props.length; i++) {
if (i > 0) {
sb.append(",");
}
if (prefix != null) {
sb.append(prefix);
sb.append(".");
}
sb.append(props[i].getName());
}
sb.append(")");
return sb.toString();
}
public String getAssocOneIdExpr(String prefix, String operator){
StringBuilder sb = new StringBuilder();
for (int i = 0; i < props.length; i++) {
if (i > 0) {
sb.append(" and ");
}
if (prefix != null){
sb.append(prefix);
sb.append(".");
}
sb.append(props[i].getName());
sb.append(operator);
}
return sb.toString();
}
public String getBindIdSql(String baseTableAlias) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < props.length; i++) {
if (i > 0) {
sb.append(" and ");
}
if (baseTableAlias != null){
sb.append(baseTableAlias);
sb.append(".");
}
sb.append(props[i].getDbColumn());
sb.append(" = ? ");
}
return sb.toString();
}
public Object convertSetId(Object idValue, EntityBean bean) {
// allow Map or String for concatenated id
Map<?,?> mapVal = null;
if (idValue instanceof Map<?,?>) {
mapVal = (Map<?,?>) idValue;
} else {
mapVal = MapFromString.parse(idValue.toString());
}
// Use a new LinkedHashMap to control the order
LinkedHashMap<String,Object> newMap = new LinkedHashMap<String, Object>();
for (int i = 0; i < props.length; i++) {
BeanProperty prop = props[i];
Object value = mapVal.get(prop.getName());
// Convert the property type if required
value = props[i].getScalarType().toBeanType(value);
newMap.put(prop.getName(), value);
if (bean != null) {
// support PropertyChangeSupport
prop.setValueIntercept(bean, value);
}
}
return newMap;
}
}
@@ -26,8 +26,6 @@ public final class IdBinderSimple implements IdBinder {
private final String bindIdSql;
private final BeanProperty[] properties;
private final Class<?> expectedType;
@SuppressWarnings("rawtypes")
@@ -37,8 +35,6 @@ public final class IdBinderSimple implements IdBinder {
this.idProperty = idProperty;
this.scalarType = idProperty.getScalarType();
this.expectedType = idProperty.getPropertyType();
this.properties = new BeanProperty[1];
properties[0] = idProperty;
bindIdSql = InternString.intern(idProperty.getDbColumn()+" = ? ");
}
@@ -46,33 +42,37 @@ public final class IdBinderSimple implements IdBinder {
// do nothing
}
public String getOrderBy(String pathPrefix, boolean ascending) {
public String getOrderBy(String pathPrefix, boolean ascending){
StringBuilder sb = new StringBuilder();
if (pathPrefix != null){
sb.append(pathPrefix).append(".");
}
sb.append(idProperty.getName());
if (!ascending){
sb.append(" desc");
}
return sb.toString();
StringBuilder sb = new StringBuilder();
if (pathPrefix != null) {
sb.append(pathPrefix).append(".");
}
sb.append(idProperty.getName());
if (!ascending) {
sb.append(" desc");
}
return sb.toString();
}
public void buildSelectExpressionChain(String prefix, List<String> selectChain) {
public void buildSelectExpressionChain(String prefix, List<String> selectChain) {
idProperty.buildSelectExpressionChain(prefix, selectChain);
}
idProperty.buildSelectExpressionChain(prefix, selectChain);
}
/**
* Returns 1.
*/
public int getPropertyCount() {
return 1;
}
@Override
public BeanProperty getBeanProperty() {
return idProperty;
}
/**
* Returns 1.
*/
public int getPropertyCount() {
return 1;
}
public String getIdProperty() {
public String getIdProperty() {
return idProperty.getName();
}
@@ -90,128 +90,124 @@ public final class IdBinderSimple implements IdBinder {
public String getDefaultOrderBy() {
return idProperty.getName();
}
public BeanProperty[] getProperties() {
return properties;
}
public String getBindIdInSql(String baseTableAlias) {
if (baseTableAlias == null){
return idProperty.getDbColumn();
} else {
return baseTableAlias+"."+idProperty.getDbColumn();
}
}
public String getBindIdInSql(String baseTableAlias) {
if (baseTableAlias == null) {
return idProperty.getDbColumn();
} else {
return baseTableAlias + "." + idProperty.getDbColumn();
}
}
public String getBindIdSql(String baseTableAlias) {
if (baseTableAlias == null){
return bindIdSql;
} else {
return baseTableAlias+"."+bindIdSql;
}
}
public String getBindIdSql(String baseTableAlias) {
if (baseTableAlias == null) {
return bindIdSql;
} else {
return baseTableAlias + "." + bindIdSql;
}
}
public Object[] getIdValues(EntityBean bean){
return new Object[]{idProperty.getValue(bean)};
}
public Object[] getBindValues(Object idValue){
return new Object[]{idValue};
}
public Object[] getIdValues(EntityBean bean) {
return new Object[] { idProperty.getValue(bean) };
}
public String getIdInValueExprDelete(int size) {
return getIdInValueExpr(size);
public Object[] getBindValues(Object idValue) {
return new Object[] { idValue };
}
public String getIdInValueExprDelete(int size) {
return getIdInValueExpr(size);
}
public String getIdInValueExpr(int size) {
StringBuilder sb = new StringBuilder(2 * size + 10);
sb.append(" in");
sb.append(" (?");
for (int i = 1; i < size; i++) {
sb.append(",?");
}
sb.append(") ");
return sb.toString();
}
public void addIdInBindValue(SpiExpressionRequest request, Object value) {
value = convertSetId(value, null);
request.addBindValue(value);
}
public void bindId(DefaultSqlUpdate sqlUpdate, Object value) {
sqlUpdate.addParameter(value);
}
public void bindId(DataBind dataBind, Object value) throws SQLException {
if (!value.getClass().equals(expectedType)) {
value = scalarType.toBeanType(value);
}
idProperty.bind(dataBind, value);
}
public void writeData(DataOutput os, Object value) throws IOException {
idProperty.writeData(os, value);
}
public Object readData(DataInput is) throws IOException {
return idProperty.readData(is);
}
public void loadIgnore(DbReadContext ctx) {
idProperty.loadIgnore(ctx);
}
public Object readSet(DbReadContext ctx, EntityBean bean) throws SQLException {
Object id = idProperty.read(ctx);
if (id != null) {
idProperty.setValue(bean, id);
}
return id;
}
public Object read(DbReadContext ctx) throws SQLException {
return idProperty.read(ctx);
}
public void appendSelect(DbSqlContext ctx, boolean subQuery) {
idProperty.appendSelect(ctx, subQuery);
}
public String getAssocOneIdExpr(String prefix, String operator) {
StringBuilder sb = new StringBuilder();
if (prefix != null) {
sb.append(prefix);
sb.append(".");
}
sb.append(idProperty.getName());
sb.append(operator);
return sb.toString();
}
public String getAssocIdInExpr(String prefix) {
StringBuilder sb = new StringBuilder();
if (prefix != null) {
sb.append(prefix);
sb.append(".");
}
sb.append(idProperty.getName());
return sb.toString();
}
public Object convertSetId(Object idValue, EntityBean bean) {
if (!idValue.getClass().equals(expectedType)) {
idValue = scalarType.toBeanType(idValue);
}
public String getIdInValueExpr(int size) {
StringBuilder sb = new StringBuilder(2*size+10);
sb.append(" in");
sb.append(" (?");
for (int i = 1; i < size; i++) {
sb.append(",?");
}
sb.append(") ");
return sb.toString();
if (bean != null) {
// support PropertyChangeSupport
idProperty.setValueIntercept(bean, idValue);
}
public void addIdInBindValue(SpiExpressionRequest request, Object value) {
value = convertSetId(value, null);
request.addBindValue(value);
}
public void bindId(DefaultSqlUpdate sqlUpdate, Object value) {
sqlUpdate.addParameter(value);
}
public void bindId(DataBind dataBind, Object value) throws SQLException {
if( !value.getClass().equals(expectedType) ){
value = scalarType.toBeanType(value);
}
idProperty.bind(dataBind, value);
}
public void writeData(DataOutput os, Object value) throws IOException {
idProperty.writeData(os, value);
}
public Object readData(DataInput is) throws IOException {
return idProperty.readData(is);
}
public void loadIgnore(DbReadContext ctx) {
idProperty.loadIgnore(ctx);
}
public Object readSet(DbReadContext ctx, EntityBean bean) throws SQLException {
Object id = idProperty.read(ctx);
if (id != null){
idProperty.setValue(bean, id);
}
return id;
}
public Object read(DbReadContext ctx) throws SQLException {
return idProperty.read(ctx);
}
public void appendSelect(DbSqlContext ctx, boolean subQuery) {
idProperty.appendSelect(ctx, subQuery);
}
public String getAssocOneIdExpr(String prefix, String operator){
StringBuilder sb = new StringBuilder();
if (prefix != null){
sb.append(prefix);
sb.append(".");
}
sb.append(idProperty.getName());
sb.append(operator);
return sb.toString();
}
public String getAssocIdInExpr(String prefix) {
StringBuilder sb = new StringBuilder();
if (prefix != null) {
sb.append(prefix);
sb.append(".");
}
sb.append(idProperty.getName());
return sb.toString();
}
public Object convertSetId(Object idValue, EntityBean bean) {
if (!idValue.getClass().equals(expectedType)){
idValue = scalarType.toBeanType(idValue);
}
if (bean != null) {
// support PropertyChangeSupport
idProperty.setValueIntercept(bean, idValue);
}
return idValue;
}
return idValue;
}
}
@@ -185,8 +185,16 @@ public class DeployBeanPropertyLists {
return null;
}
public BeanProperty[] getId() {
return (BeanProperty[]) ids.toArray(new BeanProperty[ids.size()]);
public BeanProperty getId() {
if (ids.size() > 1) {
String msg = "Ebean does not support multiple @Id properties. You need to convert to using an @EmbeddedId."
+" Please email the ebean google group if you need further clarification.";
throw new IllegalStateException(msg);
}
if (ids.isEmpty()) {
return null;
}
return ids.get(0);
}
public BeanProperty[] getNonTransients() {