#317 - RawSql getting error - Caused by: java.sql.SQLException: Column Index out of range, 0 < 1.

This commit is contained in:
rbygrave
2015-07-09 21:44:45 +12:00
parent 34a0f16c74
commit b4008da70c
4 changed files with 73 additions and 4 deletions
@@ -480,6 +480,13 @@ public final class RawSql implements Serializable {
this.queryHashCode = hc;
}
/**
* Return true if the property is mapped.
*/
public boolean contains(String property) {
return this.propertyColumnMap.containsKey(property);
}
/**
* Creates an immutable copy of this ColumnMapping.
*
@@ -265,7 +265,7 @@ public class CQueryBuilder implements Constants {
propertyName = SplitName.parent(propertyName);
} else if (beanProperty instanceof BeanPropertyAssocOne<?>) {
String msg = "Column [" + column.getDbColumn() + "] mapped to complex Property[" + propertyName + "]";
msg += ". It should be mapped to a simple property (proably the Id property). ";
msg += ". It should be mapped to a simple property (probably the Id property). ";
throw new PersistenceException(msg);
}
if (propertyName != null) {
@@ -284,8 +284,16 @@ public class CQueryBuilder implements Constants {
detail.getChunk(path, true).setDefaultProperties(null, props);
}
// check if @Id property included in RawSql
boolean rawNoId = true;
BeanProperty idProperty = descriptor.getIdProperty();
if (idProperty != null && columnMapping.contains(idProperty.getName())) {
// contains the @Id property for the root level bean
rawNoId = false;
}
// build SqlTree based on OrmQueryDetail of the RawSql
return new SqlTreeBuilder(request, predicates, detail).build();
return new SqlTreeBuilder(request, predicates, detail, rawNoId).build();
}
private SqlLimitResponse buildSql(String selectClause, OrmQueryRequest<?> request, CQueryPredicates predicates, SqlTree select) {
@@ -66,15 +66,21 @@ public class SqlTreeBuilder {
private final boolean rawSql;
/**
* rawNoId true if the RawSql does not include the @Id property
*/
private final boolean rawNoId;
private SqlTreeNode rootNode;
/**
* Construct for RawSql query.
*/
public SqlTreeBuilder(OrmQueryRequest<?> request, CQueryPredicates predicates, OrmQueryDetail queryDetail) {
public SqlTreeBuilder(OrmQueryRequest<?> request, CQueryPredicates predicates, OrmQueryDetail queryDetail, boolean rawNoId) {
this.rawSql = true;
this.desc = request.getBeanDescriptor();
this.rawNoId = rawNoId;
this.query = null;
this.subQuery = false;
this.queryDetail = queryDetail;
@@ -95,6 +101,7 @@ public class SqlTreeBuilder {
OrmQueryRequest<?> request, CQueryPredicates predicates) {
this.rawSql = false;
this.rawNoId = false;
this.desc = request.getBeanDescriptor();
this.query = request.getQuery();
@@ -257,7 +264,7 @@ public class SqlTreeBuilder {
// Optional many property for lazy loading query
BeanPropertyAssocMany<?> lazyLoadMany = (query == null) ? null : query.getLazyLoadForParentsProperty();
boolean withId = !subQuery && (query == null || !query.isDistinct());
boolean withId = !rawNoId && !subQuery && (query == null || !query.isDistinct());
return new SqlTreeNodeRoot(desc, props, myList, withId, includeJoin, lazyLoadMany);
} else if (prop instanceof BeanPropertyAssocMany<?>) {